-
-
Save gfx/2011642 to your computer and use it in GitHub Desktop.
This file contains 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 characters
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 ] } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment