Last active
August 29, 2015 14:00
-
-
Save kikuchy/11031632 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
var obj1 = {hoge: "moge"}; | |
var obj2 = {hoge: "moge"}; | |
function changePart(arg){ | |
arg.hoge = "piyo"; | |
} | |
function changeWhole(arg){ | |
arg = {foo: "bar"}; | |
} | |
console.log("JSは共有渡しなので、オブジェクトのメンバは変更可能だが") | |
console.log(obj1); | |
changePart(obj1); | |
console.log(obj1); | |
console.log("オブジェクトを丸々入れ替えるのは無理") | |
console.log(obj2); | |
changeWhole(obj2); | |
console.log(obj2); | |
/* | |
JSは共有渡しなので、オブジェクトのメンバは変更可能だが | |
{ hoge: 'moge' } | |
{ hoge: 'piyo' } | |
オブジェクトを丸々入れ替えるのは無理 | |
{ hoge: 'moge' } | |
{ hoge: 'moge' } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment