2014-09-21 61 views
1

请原谅我的javascript无知:为什么我不能在JavaScript中做这样的事情?运行这个告诉我没有定义Called。这些功能的顺序当然不重要。为什么我不能从另一个对象内调用另一种方法

var myObj = { 
 
    theCaller: function() { 
 
    console.log('The Caller'); 
 
    theCalled(); 
 
    }, 
 

 
    theCalled: function() { 
 
    console.log("i was called"); 
 
    } 
 
} 
 

 
myObj.theCaller();

+6

使用this.theCalled() – dandavis 2014-09-21 03:02:34

+0

感谢dandavis。这是因为JavaScript没有块范围?此外,这是一个可以接受的方式,比如说构建一个对象,或者我使用面向对象的思想,当JavaScript有不同的方式做同样的事情? – user1003295 2014-09-21 03:44:27

回答

1

添加 “这个” 你所拨打.theCalled()之前

var myObj = { 
 
    theCaller: function() { 
 
    alert('The Caller'); 
 
    this.theCalled(); 
 
    }, 
 
    theCalled: function() { 
 
    alert("i was called"); 
 
    } 
 
} 
 

 
myObj.theCaller();

相关问题