2012-02-07 47 views
1

好吧,刚刚解决了一个问题,其中this refered to the wrong scope。现在我有另一个问题。JavaScript是指方法内的一种方法?

所以我要调用一个方法一个方法。但我不知道怎么回事,检查此来源:

function someObj() { 
    var self = this; 

    this.someMethod1 = function() { 
     var elementBtn = document.getElementById('myBtn'); 

     elementBtn.onclick = function() { 
     self.someMethod2.methodMethod(); 
     //I want this.someMethod2.methodMethod() to be called 
     //...but I get an big error instead. Is it even possible? 
     //this.someMethod2() works fine. 

     }; 
    }; 
    this.someMethod2 = function() { 
     this.methodMethod = function() { 
     alert('THIS IS THE ONE I WANTED!'); 
     }; 
     alert('NO, NOT THIS!'); 
    }; 
} 

错误消息:

Uncaught TypeError: Object function() { ...

回答

3

与您的代码,需要先执行someMethod2才能分配函数表达式。即使这样,它也会被分配给父实例。

铭记,所有的功能都在JavaScript对象,这是你想要什么,而不是:

this.someMethod2 = function() { 
    alert('NO, NOT THIS!'); 
}; 
this.someMethod2.methodMethod = function() { 
    alert('THIS IS THE ONE I WANTED!'); 
}; 
0

虽然这不是直接可行的,你可以通过一个“命令”外部函数告诉它执行内部函数。但是,你确定这是你真正需要的吗?也许你应该在这里使用对象而不是函数。但这里是“命令”方式:

this.someMethod2 = function(cmd) { 
    var methodMethod = function() { 
     alert('THIS IS THE ONE I WANTED!'); 
    }; 

    if (cmd === "methodMethod") { 
     methodMethod(); 
     return; 
    } 

    alert('NO, NOT THIS!'); 
}; 
+0

你能不能举个例子?我对JS中的对象和函数有些困惑。 ^^ – jamietelin 2012-02-07 09:19:32

2

您正试图在函数上使用对象存取器。如果您希望以这种方式工作,则需要将呼叫中的对象文字返回到“外部”功能。

this.someMethod2 = function() { 
    return { 
    methodMethod: function() { 
     alert('THIS IS THE ONE I WANTED!'); 
    } 
    } 
}; 

然后,您可以链接呼叫。 self.someMethod2().methodMethod();

+0

但是,然后调用self.someMethod2()不会做得太多吗? – jamietelin 2012-02-07 09:24:30

+1

调用self.someMethod2()将返回对象文字。你不能把某些东西当作一个函数和一个对象文字。 – Gazler 2012-02-07 09:26:31

0

试图

function someObj() { 
var self = this; 

this.someMethod1 = function() { 
    var elementBtn = document.getElementById('myBtn'); 
    elementBtn.onclick = function() { 
     self.someMethod2().methodMethod(); 
    }; 

this.someMethod2 = function() { 

    this.methodMethod = function() { 
    alert('THIS IS THE ONE I WANTED!'); 
    }; 
    alert('NO, NOT THIS!'); 
    return this; 
}; 
} 

此外,如果你使用原型然后

function someObj() { 
var self = this; 

    this.someMethod1 = function() { 
    var elementBtn = document.getElementById('myBtn'); 
    elementBtn.onclick = function() { 
     self.someMethod2.methodMethod();//['methodMethod'](); 
    }; 
    }; 

    this.someMethod2 = function() { 


}; 
this.someMethod2.methodMethod = function() { 
    alert('THIS IS THE ONE I WANTED!'); 
    }; 
}; 

但这种方法methodMethod是静态

0
function someObj() { 
    var self = this; 

    this.someMethod1 = function() { 
     var elementBtn = document.getElementById('myBtn'); 

     elementBtn.onclick = function() { 
      self.someMethod2().methodMethod(); 
     }; 
    }; 
    this.someMethod2 = function() { 
     this.methodMethod = function() { 
      alert('THIS IS THE ONE I WANTED!'); 
     }; 
     //return this for chain method. 
     return this; 
    }; 
}