我无法访问对象的属性。
console.log(object)
给我这个并显示定义的“显示”属性
但这样做会返回undefined
console.log(object.display)
如何访问显示对象?
非常感谢!
var classOne = new Class({
myOtherFunction : function () {
this.display = new classTwo();
}
});
var classTwo = new Class({
myFunction : function () {
//does something
this.fireEvent('customEvent');
}
});
var classThree = new Class({
initialize: function () {
// THIS IS WHERE MY PROBLEM IS
class_one.display.addEvent('customEvent', function () {
this.doSomething();
}.bind(this));
}
});
class_one = new classOne();
class_three = new classThree();
这不是确切的代码,但我删除了很多额外的东西。谢谢!
答案 0 :(得分:0)
如果剥离版本真的具有代表性:
class_one
在classThree
内被引用,但之前未被声明。
display
仅在调用class_one.myOtherFunction
时初始化。...但你应该给我们更多关于JsFiddle的代码,以确保你真正采用了代表性的部分。
我要说的最好的办法是了解Javascript variables instanciation and referencing以获得更好的信息,否则你会继续遇到这些问题。
PS:您应该选择CamelCasing
或using_underscores_for_variables
。如果是camelcasing(首选),则类应为UpperCasedCamelCase
,变量lowerCaseCamelCase
。