在其对象中调用对象的方法(Calling an object's method within its object)
我有这样的代码,我使用函数在对象中创建对象。 如您所见,我无法访问createObj,因为它尚未定义。 使用“this”并没有帮助,因为它仍然是“窗口”。 我知道一种替代方法是在创建对象o之后使用New或创建“obj1”属性,但有没有其他方法可以做到这一点?
o = { createObj: function(){ //obj creation code }, obj1: createObj() }I have my code like this where I am creating objects within an object using a function. As you can see I cannot access createObj as it is not yet defined. Using "this" did not help because the it will still be "window". I know one alternative is to use the New or create "obj1" property after creating object o, but is there any other way to do this?
o = { createObj: function(){ //obj creation code }, obj1: createObj() }最满意答案
你可以;
var o = (function() { function createObj() { //obj creation code } return { createObj: createObj, obj1: createObj() } })();You could;
var o = (function() { function createObj() { //obj creation code } return { createObj: createObj, obj1: createObj() } })();更多推荐
发布评论