mootools 类无法访问对象,但我可以在控制台中看到它
我无法访问对象的属性。
console.log(object)
给我这个并显示“显示”属性已定义
但这样做会返回未定义
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();
这不是确切的代码,但我删除了很多额外的东西。谢谢!
I cannot access an object's property.
console.log(object)
Gives me this and shows be the the "display" property is defined
But doing this returns undefined
console.log(object.display)
How can I access the display object?
Many thanks!
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();
This is not the exact code but I removed a lot of the extra stuff. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果该剥离版本确实具有代表性:
class_one
在classThree
内部被引用,而无需事先声明。display
仅在调用class_one.myOtherFunction
时初始化。…但是您应该在 JsFiddle 上给我们更多代码,以确保您确实获取了代表性部分。
我想说最好的办法是了解更多关于 Javascript 变量实例化和引用正确处理这一点,否则您将继续遇到此类问题。
PS:您应该选择
CamelCasing
或using_underscores_for_variables
。如果采用驼峰式命名(首选),则类应为UpperCasedCamelCase
,变量应为lowerCaseCamelCase
。If that stripped version is really representative:
class_one
is referenced insideclassThree
without having been declared previously.display
is initialized only whenclass_one.myOtherFunction
is called.…but you should give us more code on JsFiddle to make sure you actually took representative parts.
I'd say the best thing to do is to learn more about Javascript variables instanciation and referencing to get this right, otherwise you will keep on encountering such problems.
PS: you should chose either
CamelCasing
orusing_underscores_for_variables
. If camelcasing (preferred), then classes should beUpperCasedCamelCase
, variableslowerCaseCamelCase
.