在 IE8 中初始化另一个对象内的对象失败

发布于 2025-01-06 02:52:17 字数 1258 浏览 4 评论 0原文

我想实现一种单例对象(下面示例中的About),它本身位于另一个对象(Main)内。

这是我的代码。它适用于所有主要浏览器(Firefox、Chrome 甚至 IE9),但不适用于 IE8。在 IE8 中,调用 main.About.doSomething(); 会抛出“对象不支持此属性或方法”。

我还在这里 jsFiddled 我的代码: http://jsfiddle.net/c3URh/12/

我该怎么办需要让它在 IE8 中工作吗?

注意:我可以调用 main.About().doSomething() ,它可以在 IE8 中工作,但不能在其他浏览器中工作,无论如何,从 OOP 来看这是不正确的看法。

我的错误代码:

function About(){
    this.doSomething = function(){
        alert('a');
    };
}

function Main(){
    var about;
    try {
    Object.defineProperty(this, 'About', {
            get: function () {
                if (about == undefined) {
                    about = new About();
                }
                return about;
            }
        });
    }
    catch (e) {
        // this code does not work in ie8. Throwing 'Object doesn't support this property or method'
        this.About = function() {
            if (about == undefined) {
                about = new About();
            }
            return about;
        };
    }
}

function clickMe()
{
    var main = new Main();
    main.About.doSomething();
}
​

I want to implement a sort of a singleton object (About in the example below), which by itself is inside another object (Main).

This is my code. It works in every major browser (Firefox, Chrome and even IE9) but not in IE8. In IE8, the call to main.About.doSomething(); throws 'Object doesn't support this property or method'.

I also jsFiddled my code here: http://jsfiddle.net/c3URh/12/

What do I need in order to get it to work in IE8?

Note: I can call main.About().doSomething() and it will work in IE8, but won't work in other browsers, and anyway it's incorrect from OOP perspective.

My buggy code:

function About(){
    this.doSomething = function(){
        alert('a');
    };
}

function Main(){
    var about;
    try {
    Object.defineProperty(this, 'About', {
            get: function () {
                if (about == undefined) {
                    about = new About();
                }
                return about;
            }
        });
    }
    catch (e) {
        // this code does not work in ie8. Throwing 'Object doesn't support this property or method'
        this.About = function() {
            if (about == undefined) {
                about = new About();
            }
            return about;
        };
    }
}

function clickMe()
{
    var main = new Main();
    main.About.doSomething();
}
​

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

音盲 2025-01-13 02:52:17

IE8 不支持Object.defineProperty。因此,catch 块的代码被执行。在该块中,您没有为 About getter 定义正确的替换。

这个(在 catch 内部)是一个函数:

    this.About = function() {
        if (about == undefined) {
            about = new About();
        }
        return about;
    };

虽然您期望它是 About 的实例。 IE8不支持getter,所以你必须使用另一种方法。您可以获得的最接近的是:

    this.About = about == undefined ? new About() : about;

IE8 does not support Object.defineProperty. So, the catch block's code is executed. In that block, you did not define a proper replacement for the About getter.

This (inside catch) is a function:

    this.About = function() {
        if (about == undefined) {
            about = new About();
        }
        return about;
    };

While you expecte it to be an instance of About. IE8 doesn't support getters, so you have to use another method. The closest you can get is:

    this.About = about == undefined ? new About() : about;
梨涡少年 2025-01-13 02:52:17

是不是因为defineProperty失败了,所以没有About可以添加? IE8 仅部分支持 DefineProperty,您可以通过 google 或 SO 搜索找到: 工作围绕 IE8 损坏的 Object.defineProperty 实现

http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx

Is that not because the defineProperty fails and therefore there is no About to add to? IE8 has only partial support for defineProperty which you could find via google or SO search: Working around IE8's broken Object.defineProperty implementation

http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx

青巷忧颜 2025-01-13 02:52:17

IE9 之前没有 getter,这段代码看起来真的很有趣。使用 getter 实例化私有变量,并添加检查以便仅在第一次执行?这就是构造函数的用途。

function About(){
    this.doSomething = function(){
        alert('a');
    };
}

function Main(){
    this.About = new About();
}

var main = new Main();
main.About.doSomething();  // alerts 'a'

这并不能解决您如何在 IE8 及更低版本中实现 getter 的问题,但您无论如何都以不好的方式使用它

http://jsfiddle.net/mendesjuan/zPq4v/

There are no getters before IE9 and this code is really funny looking. Using a getter to instantiate a private variable, and adding a check so it only does it the first time? That's what constructors are for.

function About(){
    this.doSomething = function(){
        alert('a');
    };
}

function Main(){
    this.About = new About();
}

var main = new Main();
main.About.doSomething();  // alerts 'a'

This doesn't solve your problem of how to implement getters in IE8 and below, but you were using it in a bad way anyhow

http://jsfiddle.net/mendesjuan/zPq4v/

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