克隆或应用:哪个“更好”?

发布于 2024-12-14 20:28:29 字数 1084 浏览 1 评论 0原文

我想创建一系列从基础对象继承或复制实例属性的对象。这使我决定使用哪种模式,我想询问您关于哪种方法“更好”的意见。

//APPLY:
// ---------------------------------------------------------
//base object template
var base = function(){
   this.x = { foo: 'bar'};
   this.do = function(){return this.x;}
}

//instance constructor
var instConstructor = function (a,b,c){
   base.apply(this);//coerces context on base
   this.aa = a;
   this.bb = b;
   this.cc = c;
}

instConstructor.prototype = new base();

var inst = function(a,b,c){
    return new instConstructor(a,b,c);
}

//CLONE   
// --------------------------------------------------------- 
function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}    

var x = {foo: 'bar'};

function instC(a,b,c){
     this.aa = a;
     this.bb = b;
     this.cc = c;
     this.x = clone(x);
};

instC.prototype.do = function(){
    return this.x;
}

它们都实现了相同的目标,即基于通用模板的独特实例属性 - 问题是哪个更“优雅”

I want to create a series of objects that inherit or copy instance properties from a base object. This has led me to a decision about which pattern to use, and I wanted to ask your opinions about which method was "better".

//APPLY:
// ---------------------------------------------------------
//base object template
var base = function(){
   this.x = { foo: 'bar'};
   this.do = function(){return this.x;}
}

//instance constructor
var instConstructor = function (a,b,c){
   base.apply(this);//coerces context on base
   this.aa = a;
   this.bb = b;
   this.cc = c;
}

instConstructor.prototype = new base();

var inst = function(a,b,c){
    return new instConstructor(a,b,c);
}

//CLONE   
// --------------------------------------------------------- 
function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}    

var x = {foo: 'bar'};

function instC(a,b,c){
     this.aa = a;
     this.bb = b;
     this.cc = c;
     this.x = clone(x);
};

instC.prototype.do = function(){
    return this.x;
}

they both achieve the same thing, namely unique instance properties based on a common template - the question is which one is more "elegant"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2024-12-21 20:28:29

从你的问题的声音来看,你似乎正在寻找类似于 Object 的东西.创建。此函数可用于创建一个新对象,该新对象以您选择的对象作为原型。

//for old browsers without Object.create:
var objCreate = function(proto){
    function F(){};
    F.prototype = proto;
    return new F();

至于与克隆方法的比较,它们做的事情不同,所以你必须选择更合适的。

使用原型会将父对象的更改反映到子对象上。

a = {x:1};
b = Object.create(a);
a.x = 2;
//b.x is now 2 as well

您还必须小心使用具有 this 的方法。根据您的操作,如果您使用过多的原型继承,可能会产生不良后果。

a ={
   x: 1,
   foo: function(){ this.x += 1 }
}
b = Object.create(a);
b.foo();
//a.x does not change

另一方面,克隆会克隆一些东西,这样您就可以确保这些对象不会以任何方式相互干扰。有时这就是你想要的。

By the sound of your questions it looks like you are looking for something akin to Object.create. This function can be used to create a new object that has an object of your choice as a prototype.

//for old browsers without Object.create:
var objCreate = function(proto){
    function F(){};
    F.prototype = proto;
    return new F();

As for the comparison with the clone method, they do different things so you have to choose what is more appropriate.

Using prototypes will reflect changes on the parent object on the child objects.

a = {x:1};
b = Object.create(a);
a.x = 2;
//b.x is now 2 as well

You also have to be careful about using methods that have this. Depending on what you do you can have unwanted consequences if you use too much prototypal inheritance.

a ={
   x: 1,
   foo: function(){ this.x += 1 }
}
b = Object.create(a);
b.foo();
//a.x does not change

On the other hand, cloning clones stuff so you can be sure that the objects won't interfere with each other in any way. Sometimes this is what you want.

远昼 2024-12-21 20:28:29

好的,所以我对此进行了进一步的思考 - 答案源于用例。克隆将属性复制到给定范围,而应用则更改属性周围的范围。前者更适合 mixin,后者更适合继承。

OK, so i've given this some further thought - and the answer stems from the use case. Where clone is copying properties into a given scope, apply is changing the scope surrounding the property. The former is better for mixins, the latter for inheritance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文