Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向原型添加属性的最佳模式是什么_Javascript - Fatal编程技术网

Javascript 向原型添加属性的最佳模式是什么

Javascript 向原型添加属性的最佳模式是什么,javascript,Javascript,下面是我需要的三个添加方法的选项 原型。我想知道是否有什么问题 任何一种模式。使用这样的原型会有不同吗?或者它们都一样,只是写得不同而已 每次运行选项后,我都可以调用egg().alertMe() 这些只是简单的提问方式 谢谢你的帮助 // option 1 // Methods are added to the egg functions prototype // individually and the newly created // objects prototype points to

下面是我需要的三个添加方法的选项 原型。我想知道是否有什么问题 任何一种模式。使用这样的原型会有不同吗?或者它们都一样,只是写得不同而已

每次运行选项后,我都可以调用
egg().alertMe()

这些只是简单的提问方式

谢谢你的帮助

// option 1
// Methods are added to the egg functions prototype
// individually and the newly created
// objects prototype points to this prototype.
var egg = function() {

        return new egg.init();

};

egg.prototype.alertMe = function () {alert('alertMe');};
egg.prototype.logMe = function () {console.log('logMe');};

// constructor function
egg.init = function() {};

egg.init.prototype = egg.prototype;



我更喜欢
Object.assign(egg.prototype,{…})
@elclanrs
Object.assign
非常棒,但是如果您尝试使用getter/setter,请小心,因为它会复制getter的结果。另外,为什么问题中没有
对象。创建
选项?谢谢你的回答,因为这是用于编写jquery库的模式类型,我正在尝试理解原型。同样在阅读MDN上复制的Object.assign()之后,我只想引用而不是复制。区别在于
prototype.constructor
egg.prototype==init.prototype
。除此之外,:-)谢谢你说的反模式是什么意思?我更喜欢
Object.assign(egg.prototype,{…})
@elclanrs
Object.assign
非常棒,但是如果你试图使用getter/setter,请小心,因为它会复制getter的结果。另外,为什么问题中没有
对象。创建
选项?谢谢你的回答,因为这是用于编写jquery库的模式类型,我正在尝试理解原型。同样在阅读MDN上复制的Object.assign()之后,我只想引用而不是复制。区别在于
prototype.constructor
egg.prototype==init.prototype
。除此之外,:-)谢谢你说的反模式是什么意思?
// option 2
// Methods are added to the egg functions prototype
// in an object literal and the newly created
// objects prototype points to this prototype.
var egg = function() {

        return new egg.init();

};

egg.prototype = {
        // Alert me.
        alertMe: function () {alert('alertMe');},
        // Log me.
        logMe: function () {console.log('logMe');}
    };

// constructor function
egg.init = function() {};

egg.init.prototype = egg.prototype;
// option 3
// Just add the methods to the the newly created
// objects prototype.

var egg = function() {

        return new egg.init();

};

// constructor function
egg.init = function() {};


egg.init.prototype = {
        // Alert me.
        alertMe: function () {alert('alertMe');},
        // Log me.
        logMe: function () {console.log('logMe');}
};