Created
October 18, 2012 08:48
-
-
Save haiiro-shimeji/3910525 to your computer and use it in GitHub Desktop.
prototypeのプロパティにオブジェクトを代入しちゃだめ
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 characters
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