jquery - Cloning JavaScript Objects. Again :( -
I know that this topic is really annoying to read again before the code digging can be a solution That I did not get the prototype and the object in javascript. . But at this point I think that, I do
have the problem:
Javascript class (created with prototype), how to clone so that "clone" class When the extension remains untouched and executed later?
function clone (obz) {if (obj == faucet} type ob (obje)! = 'Object') change obe; Var temp = new obj.constructor (); For (var key in obj) temp [key] = clon (obj [key]); Return temporary; } Var FOO = function () {var myBAR = clone (bar); MyBAR.prototype = jQuery.extend (true, myBAR.prototype, this); // Double cloning twice and extension with this console.log ("FOO:", this.name); New mieber (); }; FOO.prototype = {name: "FOO"}; Var BAR = function () {console.log ("BAR:", this.name); }; BAR.prototype = {name: "bar"}; New FOO (); // imports FOO: FOO and bar: FOO new bar (); // Return time: FOO should be repeated back:
If I got it right then the second call of new bar ()
( New FOO ()
) Bar: Bar
No Bar: FOO
should be in the form of this time.
Function clone (obj) {
Return eval ("(+ + obj.toString () + '); // Similar to eval (abnormal (obj));}
But a big negative side of this approach You can not pass any dynamically created objects.
Any ideas?
How are you cloning 'prototype'
The following line
MyBAR.prototype = jQuery.extend (true, myBAR.prototype, this); // deep cloning
You only use 'prototype' Do not cloning, you are also cloning the 'name' property.
Instead of the above line with you
myBAR.prototype = jQuery.extend (True, myBAR.prototype, this.prototype); // dark cloning
your code will now return
new FOO (); // import Does FOO: FOO and Bar: times new times (); // Return time: Bar
Comments
Post a Comment