由于在声明函数时,我们会使其原型的构造函数指向该函数本身,因此像这样覆盖函数的原型是否是一种不好的做法:
function LolCat() {
}
// at this point LolCat.prototype.constructor === LolCat
LolCat.prototype = {
hello: function () {
alert('meow!');
}
// other method declarations go here as well
};
// But now LolCat.prototype.constructor no longer points to LolCat function itself
var cat = new LolCat();
cat.hello(); // alerts 'meow!', as expected
cat instanceof LolCat // returns true, as expected
这不是我的做法,我仍然更喜欢以下的方法
LolCat.prototype.hello = function () { ... }
但我经常看到其他人这样做。
那么,通过覆盖函数的原型对象以方便起见从原型中删除构造函数引用是否会产生任何影响或缺点,就像第一个例子中那样?