Javascript 从JS中的另一个函数调用方法(TypeError异常)

Javascript 从JS中的另一个函数调用方法(TypeError异常),javascript,Javascript,我想这确实是一个新手的错误,但我不能让它运行。 我有一个“计算器”对象t,它包含许多计算值的函数。我需要从我的“calculator”对象中使用这些函数来获取另一个函数中的一些值。 我将其分条如下,但在调用t.hello()方法时得到一个TypeError异常。有什么想法吗 var t = new T(); two(); function T() { function hello() { alert("hello"); } } function

我想这确实是一个新手的错误,但我不能让它运行。 我有一个“计算器”对象t,它包含许多计算值的函数。我需要从我的“calculator”对象中使用这些函数来获取另一个函数中的一些值。 我将其分条如下,但在调用t.hello()方法时得到一个TypeError异常。有什么想法吗

 var t = new T();
 two();

 function T() {
     function hello() {
         alert("hello");
     }
 }

 function two() {

     t.hello();

 }

您需要返回包含函数的对象:

function T() {
    return {
        'hello': function () {
            alert("hello");
        }
    }
}
或者专门将其定义为
T
范围内的函数:

function T() {
    this.hello = function() {
        alert("hello");
    }
}

您需要返回包含函数的对象:

function T() {
    return {
        'hello': function () {
            alert("hello");
        }
    }
}
或者专门将其定义为
T
范围内的函数:

function T() {
    this.hello = function() {
        alert("hello");
    }
}

函数
hello
T
的本地范围内

这样定义
T

function T() {
     this.hello = function() {
         alert("hello");
     }
 }

函数
hello
T
的本地范围内

这样定义
T

function T() {
     this.hello = function() {
         alert("hello");
     }
 }
试试这个

var t = new T();
two();
function T() {
     this.hello = function () {
         alert("hello");
     }
 }    
 function two() {    
     t.hello();    
 } 
看看这个:

试试这个

var t = new T();
two();
function T() {
     this.hello = function () {
         alert("hello");
     }
 }    
 function two() {    
     t.hello();    
 } 

看到这个:

实际上
hello
在你的例子中不是一个方法,它是在其他函数中定义的一个局部函数实际上
hello
在你的例子中不是一个方法,它是在其他函数中定义的一个局部函数,谢谢!但还有一个问题。如何从T开始在内部调用“hello”?@audio\u developer:在第二个示例中,您可以在
T
中使用
this.hello()
。在使用第一个示例时,您不能这样做。再次感谢。我在问之前就试过了,但是打错了电话“this.hello(}”…我想这意味着我需要休息一下。:-)祝你愉快!我发现了另一个问题:这是一种从内部函数调用hello的好方式吗@音频开发者:是的,那也行。这样,您就可以创建私有函数。我明白了,谢谢!但还有一个问题。如何从T开始在内部调用“hello”?@audio\u developer:在第二个示例中,您可以在
T
中使用
this.hello()
。在使用第一个示例时,您不能这样做。再次感谢。我在问之前就试过了,但是打错了电话“this.hello(}”…我想这意味着我需要休息一下。:-)祝你愉快!我发现了另一个问题:这是一种从内部函数调用hello的好方式吗@音频开发者:是的,那也行。这样,就可以创建私有函数。