-
-
Save gfx/2011642 to your computer and use it in GitHub Desktop.
Revisions
-
gfx revised this gist
Mar 10, 2012 . 2 changed files with 31 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,19 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ function Base(x) { // override properties in prototype if necessary if (arguments.length == 1) { this._x = x; } } Base.prototype = { _x: [], push: function(v) { this._x.push(v) } }; function Derived(x) { // override properties in prototype if necessary if (arguments.length == 1) { Base.call(this, x); } } Derived.prototype = new Base([]); // default value of _x is [] for Derived var a = new Derived(), b = new Derived(); // 両方とも同じDerived.prototype._xに対して操作してしまう! a.push(10); b.push(20); console.log(a.__proto__); // { _x: [ 10, 20 ] } console.log(b.__proto__); // { _x: [ 10, 20 ] } -
kazuho created this gist
Mar 10, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ function Base(x) { // override properties in prototype if necessary if (arguments.length == 1) { this._x = x; } } Base.prototype = { _x: 1 }; function Derived(x) { // override properties in prototype if necessary if (arguments.length == 1) { Base.call(this, x); } } Derived.prototype = new Base(3); // default value of _x is 3 for Derived