我试图了解如何从另一个构造函数访问属性。例如,我想分离 App、Effects、Utils 等对象,并从一个到另一个调用属性和方法。这是可能的,还是这种方式完全错误?
var App = function() {
this.someProperty = 'Lorem';
this.init();
};
App.prototype = {
init:function(){
this.bindEvents();
},
bindEvents:function(){
var self = this;
$(window).on('resize', function(e) {
e.preventDefault();
this.windowWidth = $(window).width();
// Is that Correct?
Utils.prototype.resizeElement(this.windowWidth);
});
}
};
var Utils = function(){};
Utils.prototype = {
resizeElement: function(windowW){
console.log(windowW);
},
someMethod:function(){
// How can i access property written in App constructor?
console.log(this.someProperty);
}
};
var Effects = function(){
this.init();
};
Effects.prototype = {
hideElement:function(ele){
$(ele).hide();
}
};
var app = new App();