Javascript:覆盖函数的原型 - 坏实践? [英] Javascript: Overwriting function's prototype - bad practice?

查看:172
本文介绍了Javascript:覆盖函数的原型 - 坏实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为当我们声明一个函数时,我们得到它的原型的构造函数属性指向函数本身,这是一个糟糕的做法,重写函数的原型,如下:

  function LolCat(){
}

//此时LolCat.prototype.constructor === LolCat

LolCat.prototype = {
hello:function(){
alert('meow!');
}
//其他方法声明也在这里
};

//但是现在LolCat.prototype.constructor不再指向LolCat函数本身

var cat = new LolCat();

cat.hello(); // alerts'meow!',正如预期的

cat instanceof LolCat //如预期返回true

这不是我怎么做,我仍然喜欢以下方法

  LolCat.prototype.hello = function(){...} 

但我经常看到其他人这样做。 >

因此,为了方便起见,通过覆盖函数的原型对象,从原型中删除构造函数引用,有什么含义或缺点吗?

解决方案

我看不到有人提到最佳实践,因此我想这是由你能看到 constructor 属性是有用的。



有一件值得注意的事情是构造函数属性,如果你不销毁它,也将在创建的对象上可用。在我看来这可能是有用的:

  var ClassOne = function(){alert(created one);} 
var ClassTwo = function(){alert(created two);}

ClassOne.prototype.aProperty =hello world // preserve constructor
ClassTwo.prototype = {aProperty:hello world}; // destroy constructor

var objectOne = new ClassOne(); // alertscreated one
var objectTwo = new ClassTwo(); // alertscreated two

objectOne.constructor(); //再次创建一个警告
objectTwo.constructor(); //创建并返回一个空对象实例

所以在我看来,这是一个架构决定。您要允许已创建的对象在实例化之后重新调用其构造函数吗?如果这样保存它。请注意,objectTwo的构造函数现在完全等于标准的Object构造函数 - 无用。

  objectTwo.constructor === Object; // true 

因此调用 new objectTwo.constructor()等效于 new Object()


Since when we declare a function we get its prototype's constructor property point to the function itself, is it a bad practice to overwrite function's prototype like so:

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

This is not how I do it, I still prefer the following approach

LolCat.prototype.hello = function () { ... }

but I often see other people doing this.

So are there any implications or drawbacks by removing the constructor reference from the prototype by overwriting the function's prototype object for the sake of convenience as in the first example?

解决方案

I can't see anyone mentioning best practice as far as this is concerned, so I think it comes down to whether you can see the constructor property ever being useful.

One thing worth noting is that the constructor property, if you don't destroy it, will be available on the created object too. It seems to me like that could be useful:

var ClassOne = function() {alert("created one");}
var ClassTwo = function() {alert("created two");}

ClassOne.prototype.aProperty = "hello world"; // preserve constructor
ClassTwo.prototype = {aProperty: "hello world"}; // destroy constructor

var objectOne = new ClassOne(); // alerts "created one"
var objectTwo = new ClassTwo(); // alerts "created two"

objectOne.constructor(); // alerts "created one" again
objectTwo.constructor(); // creates and returns an empty object instance

So it seems to me that it's an architectural decision. Do you want to allow a created object to re-call its constructor after it's instantiated? If so preserve it. If not, destroy it.

Note that the constructor of objectTwo is now exactly equal to the standard Object constructor function - useless.

objectTwo.constructor === Object; // true

So calling new objectTwo.constructor() is equivalent to new Object().

这篇关于Javascript:覆盖函数的原型 - 坏实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆