Skip to content

Instantly share code, notes, and snippets.

@haiiro-shimeji
Created October 18, 2012 08:48
Show Gist options
  • Save haiiro-shimeji/3910525 to your computer and use it in GitHub Desktop.
Save haiiro-shimeji/3910525 to your computer and use it in GitHub Desktop.
prototypeのプロパティにオブジェクトを代入しちゃだめ
var Hoge = function() {}
Hoge.prototype = {
hoge: "foo",
fuga: []
}
test( "hoge", function() {
var h1 = new Hoge()
h1.hoge = "bar"
h1.fuga.push("bar")
var h2 = new Hoge()
equal( h2.hoge, "foo" )
equal( h2.fuga, [] ) //fail!
/*
* Hoge.prototype.fuga がオブジェクトなので、
* h1.fuga, h2.fuga に複製されるのは
* Hoge.prototype.fuga への参照だから.
*
*/
} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment