这会被视为Chrome错误还是Safari/Firefox宽恕?
当我创建一个闭包函数时,我给它一个任意名称,"按钮".在闭包中,我有一个函数,它是闭包创建的主对象的构造函数,也称为"Button".
闭包返回对Button的引用,可能是内部函数Button().
var closure = (function Button(){
//the closure's primary object
function Button(target) {
//constructor for our button object
}
Button.prototype = e.inherit(e.Plugin.prototype);
Button.prototype.constructor = Button;
return {
Button: Button,
};
}());
//now let's call our closure to create a new Button
var newButton = new closure.Button()
Run Code Online (Sandbox Code Playgroud)
在Safari和Firefox中,这不是问题.调用new closure.Button()返回内部Button()的一个实例.但是,在Chrome中,当内部函数名称与闭包函数名称匹配时,构造函数似乎不会被调用.返回的内容似乎是闭包函数本身.当然,当内部函数定义我稍后尝试和调用的方法时,这会导致错误.
我发布这个问题有两个原因:
1)我花了一天多的时间才找到问题的原因,Chrome报告我的Button没有方法.activate(在我的实际代码中,Button继承自提供.activate()的父对象方法).如果其他人遇到这个问题,那么他们可能会在这里找到我的问题并节省一些时间.
2)我想知道:如果这是我的错误,对闭包和闭包中的函数使用相同的名称,为什么它在Safari/Firefox中工作?更具体地说,Chrome确实以某种方式使闭包返回本身而不是预期的内部功能?这是一个错误吗?