如何从
有安全和正确的方法吗?
$(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);
答案 0 :(得分:1)
如何从methodTwo调用methodOne?
您需要访问要调用该方法的实例(您也可以从原型中获取它并应用于某些任意对象,但我想这不是您想要的)。要在methodTwo
的范围内将其公开,请将其作为参数传递给函数:
ClassTwo.prototype.methodTwo = function (one) {
one.methodOne();
};
…
var classOne = new ClassOne();
var classTwo = new ClassTwo();
classTwo.methodTwo(classOne);