![](/img/trans.png)
[英]Accessing/Calling a class method from another class in mediator pattern javascript
[英]Calling one method from another in a Javascript class
用Javascript定義類時,如何從另一個方法調用一個方法?
exports.myClass = function () {
this.init = function() {
myInternalMethod();
}
this.myInternalMethod = function() {
//Do something
}
}
上面的代碼在執行時給我以下錯誤:
ReferenceError:未定義myInternalMethod
我也嘗試了this.myInternalMethod和self.myInternalMethod,但是兩者都導致錯誤。 什么是正確的方法?
我創建了這個小提琴http://jsfiddle.net/VFKkC/在這里您可以調用myInternalMedod()
var myClass = function () {
this.init = function() {
this.myInternalMethod();
}
this.myInternalMethod = function() {
console.log("internal");
}
}
var c = new myClass();
c.init();
this.myInternalMethod()
確實可以工作:
var exports = {};
exports.myClass = function () {
this.init = function() {
this.myInternalMethod();
}
this.myInternalMethod = function() {
//Do something
}
}
var x = new exports.myClass();
x.init();
是私人會員嗎?
exports.myClass = function () {
this.init = function() {
myInternalMethod();
}
function myInternalMethod() {
//Do something
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.