Skip to content

Instantly share code, notes, and snippets.

@kikuchy
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikuchy/11031632 to your computer and use it in GitHub Desktop.
Save kikuchy/11031632 to your computer and use it in GitHub Desktop.
参照渡しとか共有渡しの例
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