mootools 类无法访问对象,但我可以在控制台中看到它

发布于 2024-12-26 14:10:30 字数 842 浏览 1 评论 0原文

我无法访问对象的属性。

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
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2025-01-02 14:10:30

如果该剥离版本确实具有代表性:

  1. class_oneclassThree 内部被引用,而无需事先声明。
  2. display 仅在调用 class_one.myOtherFunction 时初始化。

…但是您应该在 JsFiddle 上给我们更多代码,以确保您确实获取了代表性部分。

我想说最好的办法是了解更多关于 Javascript 变量实例化和引用正确处理这一点,否则您将继续遇到此类问题。

PS:您应该选择 CamelCasingusing_underscores_for_variables。如果采用驼峰式命名(首选),则类应为 UpperCasedCamelCase,变量应为 lowerCaseCamelCase

If that stripped version is really representative:

  1. class_one is referenced inside classThree without having been declared previously.
  2. display is initialized only when class_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 or using_underscores_for_variables. If camelcasing (preferred), then classes should be UpperCasedCamelCase, variables lowerCaseCamelCase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文