gpt4 book ai didi

Internet Explorer 中的 Javascript 命名函数表达式

转载 作者:搜寻专家 更新时间:2023-11-01 04:52:50 25 4
gpt4 key购买 nike

为什么以下代码在 Internet Explorer 中不起作用(目前我只在 IE8 中测试过):

(function(){
this.foo = function foo(){};

foo.prototype = {
bar:function(){
return 'bar';
}
};
})();

var x = new foo;
console.log(x.bar()) // Error: Object doesn't support this property or method

如果我将 foo 的分配更改为以下内容,代码将正常工作:

var foo = this.foo = function(){};

我想这与 IE 的 Javascript 引擎中的命名函数有关。该代码在 Chrome 和 Firefox 中运行良好。

有什么想法吗?

最佳答案

IE 在命名函数表达式方面存在很多问题。正如您在问题中所说,坚持这一点:

this.foo = function (){};

有关此主题的深入、艰苦的阅读,请查看 this link

简而言之,内部命名函数表达式被视为函数声明,并被提升到它永远不应该出现的位置。

关于Internet Explorer 中的 Javascript 命名函数表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8548840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com