【发布时间】:2013-09-11 16:28:08
【问题描述】:
如何从 methodTwo 调用 methodOne?
有没有安全正确的方法?
$(document).ready(function () {
var classOne = new ClassOne();
var classTwo = new ClassTwo();
});
var ClassOne = (function (window, document, Math, undefined) {
function ClassOne () {
}
ClassOne.prototype = {
methodOne: function () {
console.log('method one');
}
};
return ClassOne;
})(window, document, Math, undefined);
var ClassTwo = (function (window, document, Math, undefined) {
function ClassTwo () {
}
ClassTwo.prototype = {
methodTwo: function () {
// how to call?
// classOne.methodOne()
}
};
return ClassTwo;
})(window, document, Math, undefined);
【问题讨论】:
-
您忘记了 IEFE 中的构造函数
return,并且在定义它们之前就使用了它们。 -
这些方法是实例方法。
classTwo实例如何知道classOne实例来调用它的方法? -
@Bergi 我更新了示例!
标签: javascript class oop methods