简体   繁体   English

在jQuery中克隆和扩展对象?

[英]Clone and extend object in jQuery?

Ok, so here is the basics of what I want to do: 好的,所以这里是我想要做的基础知识:

var Hi = function(name){
    this.name = name;
};

Hi.prototype = {
    message: function(){
        $('body').append('Hi '+this.name);
    }
};

var hi = new Hi('There ');

Which works fine, but now I want to copy it so I can change it to say "Bye", 哪个工作正常,但现在我想复制它,所以我可以改变它说“再见”,

var Bye = Hi;
Bye.prototype.message = function(){
    $('body').append('Bye '+this.name);
};

var bye = new Bye('There');

so then to get the output of Hi There Bye There I thought that this should work: 所以然后得到Hi There Bye There的输出Hi There Bye There我认为这应该工作:

hi.message();
bye.message();

but instead the output is Bye There Bye There aka my modifications overwrite the original object. 但相反输出是Bye There Bye There我的修改覆盖了原始对象。

How can I get this to work as I expect? 我怎么能像我期望的那样让它工作? note, jQuery/ jQuery UI solutions are fine, but I would like both a vanilla and a jQuery version to understand what's going on! 请注意,jQuery / jQuery UI解决方案很好,但我想要一个vanilla和一个jQuery版本来了解正在发生的事情!

jsFiddle of my code: http://jsfiddle.net/YGa7p/ js我的代码: http//jsfiddle.net/YGa7p/

The line 这条线

var Bye = Hi;

does simply reference your original function, it does not copy. 确实只是引用你的原始功能,它不会复制。 Usually you do 通常你这样做

var Hi = function(name){
    this.name = name;
};

Hi.prototype.message = function() {
    $('body').append('Hi '+this.name);
};

var Bye = function(name){
    Hi.call(this, name); // re-call base constructor
};

Bye.prototype = new Hi(); // create base object

// overwrite Hi's message
Bye.prototype.message = function() {
    $('body').append('Bye '+this.name);
};

var hi = new Hi("there");
var bye = new Bye("there");

// See also instanceof:

// hi instanceof Hi      // true
// hi instanceof Object  // true

// bye instanceof Bye    // true
// bye instanceof Hi     // true
// bye instanceof Object // true

http://jsfiddle.net/YGa7p/1/ . http://jsfiddle.net/YGa7p/1/

In javaScript it's difficult to do OOP. 在javaScript中,很难做到OOP。 To make derived objects you will get into trouble using 'simple' methods, at least in Level 3...n inheritance. 要制作派生对象,使用“简单”方法会遇到麻烦,至少在Level 3 ... n继承中。 Please read my article on V javaScript class functions , if you're interested in extended inheritance in javaScript. 如果您对javaScript中的扩展继承感兴趣,请阅读我关于V javaScript类函数的文章。

Instantiage new object for the prototype. 实例化原型的新对象。 which solves the issue. 这解决了这个问题。 Like Bye.prototype=new Hi(); 像Bye.prototype = new Hi();

    var Hi = function(name) {
    this.name = name;
};

Hi.prototype = {
    message: function() {
        $('body').append('Hi ' + this.name);
    }
};

var hi = new Hi('There ');

var Bye = Hi;
Bye.prototype=new Hi();
Bye.prototype.message = function() {
    $('body').append('Bye ' + this.name);
};

var bye = new Bye('There');

hi.message();
bye.message();​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM