最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Best way to clone JavaScript class - Stack Overflow

programmeradmin4浏览0评论

I have a class declared the traditional way, i.e.

function MyClass() {
}

MyClass.prototype = {
};

Now I want to create a copy of that class (not a copy of the instance the class creates) but change some of the prototype methods. In other words I want to make a copy of the class with some augmentations... do I need to use inheritance for that or I it is enough to loop over and assign references to my new class for the original prototype and the new one?

I have a class declared the traditional way, i.e.

function MyClass() {
}

MyClass.prototype = {
};

Now I want to create a copy of that class (not a copy of the instance the class creates) but change some of the prototype methods. In other words I want to make a copy of the class with some augmentations... do I need to use inheritance for that or I it is enough to loop over and assign references to my new class for the original prototype and the new one?

Share Improve this question edited Mar 28, 2011 at 12:32 Saurabh Gokhale 46.4k35 gold badges137 silver badges163 bronze badges asked Mar 28, 2011 at 12:29 PassPass 1,5014 gold badges22 silver badges39 bronze badges 4
  • Are you using any javascript library? – Robert Koritnik Commented Mar 28, 2011 at 12:31
  • 1 MyClass is a function and not a class. – Gumbo Commented Mar 28, 2011 at 12:40
  • 3 @Gumbo There is really no difference in JS, just the way you look at it. – vbence Commented Mar 28, 2011 at 12:47
  • Sounds like the perfect candidate for inheritance. Maybe you just need to restructure your thoughts a little - maybe instead of one supertype and one subtype, have one (possibly abstract) supertype sharing all mon methods, and two subtypes each one implementing their own variations. If you need to call one subtype method from another, then maybe it should be in the parent type. – davin Commented Mar 28, 2011 at 12:49
Add a ment  | 

3 Answers 3

Reset to default 10

I would use normal inheritance. Try this:

var MyClass = function(){};
MyClass.prototype = {
  foo: function(){ alert('foo') },
  bar: function(){ alert('bar') }
};

var MySubClass = function(){};
MySubClass.prototype = new MyClass();
MySubClass.prototype.bar = function(){ alert('otherbar') };

var my = new MyClass();
var mysub = new MySubClass();
my.foo(); // foo
my.bar(); // bar
mysub.foo(); // foo
mysub.bar(); // otherbar

Combination inheritance (sometimes also called pseudoclassical inheritance) bines prototype chaining and constructor stealing to get the best of each approach.

function SuperType(name){
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

SuperType.prototype.sayName = function(){
    alert(this.name);
};

function SubType(name, age){          
    //inherit properties
    SuperType.call(this, name);
    this.age = age;
}

//inherit methods
SubType.prototype = new SuperType();

This is not logical to just clone your class, class mean shared charactaristics, if you want to just clone the class its event not logical instead of just cloning you should use to intainsiate its object. You should use inheritance to create sub classes of the existing class. read full article about inhertiance at

https://developer.mozilla/en/JavaScript/Guide/Inheritance_Revisited

发布评论

评论列表(0)

  1. 暂无评论