如何编写一个javascript克隆函数来复制添加的对象方法?

发布于 2024-12-04 20:39:02 字数 1262 浏览 0 评论 0原文

我有一个 javascript 对象克隆问题。我希望能够克隆已从对象原型定义的方法更改的对象方法,或在实例化后添加到对象的方法。这可能吗?

这里的设置是我定义的 javascript“类”,所以我可以编写一个特定于我的对象类的克隆方法。我只是不知道如何复制方法。

例子:

function myObject( name, att, dif ) {
    /* 'privileged' methods */
    this.attribute = function(newAtt) {  // just a getter-setter for the 'private' att member
        if(newAtt) { att = newAtt; }
        return att;
    }
    // 'public' members
    this.printName = name;
}

myObject.prototype.genericMethod = function() {
    // does what is usually needed for myObjects
}


/* Create an instance of myObject */
var object153 = new myObject( '153rd Object', 'ABC', 2 );
// object153 needs to vary from most instances of myObject:
object153.genericMethod = function() {
    // new code here specific to myObject instance object153
}
/* These instances become a collection of objects which I will use subsets of later. */


/* Now I need to clone a subset of myObjects, including object153 */
var copyOfObject153 = object153.clone();
// I want copyOfObject153 to have a genericMethod method, and I want it to be the one
// defined to be specific to object153 above.  How do I do that in my clone() method?
// The method really needs to still be called 'genericMethod', too.

I have a javascript object cloning question. I'd like to be able to clone object methods that have been altered from those defined by the object prototype, or added to the object after instantiation. Is this possible?

The setting here is a javascript 'class' defined by me, so I'm fine with writing a clone method specific to my object class. I just can't figure out how to copy methods.

Example:

function myObject( name, att, dif ) {
    /* 'privileged' methods */
    this.attribute = function(newAtt) {  // just a getter-setter for the 'private' att member
        if(newAtt) { att = newAtt; }
        return att;
    }
    // 'public' members
    this.printName = name;
}

myObject.prototype.genericMethod = function() {
    // does what is usually needed for myObjects
}


/* Create an instance of myObject */
var object153 = new myObject( '153rd Object', 'ABC', 2 );
// object153 needs to vary from most instances of myObject:
object153.genericMethod = function() {
    // new code here specific to myObject instance object153
}
/* These instances become a collection of objects which I will use subsets of later. */


/* Now I need to clone a subset of myObjects, including object153 */
var copyOfObject153 = object153.clone();
// I want copyOfObject153 to have a genericMethod method, and I want it to be the one
// defined to be specific to object153 above.  How do I do that in my clone() method?
// The method really needs to still be called 'genericMethod', too.

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

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

发布评论

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

评论(2

说不完的你爱 2024-12-11 20:39:02

在克隆函数中,测试对象上的每个方法,看看它是否等于对象构造函数原型上的相同方法。

if (obj[method] != obj.constructor.prototype[method])
    clone[method] = obj[method];

In your clone function, test each method on the object to see if it is equal to the same method on the object's constructor's prototype.

if (obj[method] != obj.constructor.prototype[method])
    clone[method] = obj[method];
蓝咒 2024-12-11 20:39:02

听起来你只想要一个浅拷贝。但是请注意,对象是在实例之间共享的,因为我们不是深度复制。

function clone(obj) {
   var newObj = new obj.constructor();
   for (var prop in obj) {
     newObj[prop] = obj[prop];
   }
   return newObj;
}
var cloned = clone(object153);

一种不同的语法是

myObj.prototype.clone = function() {
   var newObj = new this.constructor();
   for (var prop in this) {
     newObj[prop] = this[prop];
   }
   return newObj;
}
var cloned = object153.clone();

尝试一下,看看它是否适合您,但仍然很难判断您在做什么。如果没有,请解释原因,这样我就能更好地理解问题。

It sounds like you just want a shallow copy. However beware of that objects are shared among instances since we're not deep copying.

function clone(obj) {
   var newObj = new obj.constructor();
   for (var prop in obj) {
     newObj[prop] = obj[prop];
   }
   return newObj;
}
var cloned = clone(object153);

A different syntax would be

myObj.prototype.clone = function() {
   var newObj = new this.constructor();
   for (var prop in this) {
     newObj[prop] = this[prop];
   }
   return newObj;
}
var cloned = object153.clone();

Try it out and see if it works for you, it's still hard to tell what you're doing. If it doesn't, explain why, then I can better understand the problem.

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