Last active
August 29, 2015 14:18
-
-
Save Shinpeim/556ab84ccae6501cf57b 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 array = [1, 2, 3]; | |
function reAssign(a) { | |
// aに再代入 | |
a = []; | |
} | |
reAssign(array); | |
// reAssignの中の a と 外のarrayは「別の変数」だから | |
// aにほかのものを代入してもarrayには影響がない。 | |
console.log(array); // [1, 2, 3] | |
// しかし…… | |
function removeFirstElement(a) { | |
a.shift(); // aのラベルがついた配列を操作 | |
} | |
removeFirstElement(array); | |
// aとarrayは「別の変数」だけど、「中身」は同じものを指している。 | |
// なので、aの中身を変更すると、arrayの中身も変わる | |
console.log(array); // [2, 3] | |
////////////////////////////////////////////////////////////// | |
// 変数というのはよく「箱」に例えられるけど、どちらかというと | |
// 「ラベル」に近い。 | |
// ラベルだと思ってもう一度動きを追います。 | |
////////////////////////////////////////////////////////////// | |
var array = [1, 2, 3]; // [1, 2, 3]にarrayというラベルをつける | |
function reAssign(a) { | |
// 引数の内容に aというラベルをつけて関数本体を実行する | |
a = []; // aのラベルを [] というデータに付け替える | |
} | |
reAssign(array); // arrayのラベルがついた配列を関数に渡す | |
console.log(array); // [1, 2, 3] | |
function removeFirstElement(a) { | |
// 引数の内容に a というラベルをつけて関数本体を実行する | |
a.shift(); // aがさす配列([1, 2, 3])のshiftメソッドを実行することで、その配列のデータが変化する | |
} | |
removeFirstElement(array); //arrayのラベルのついた配列を関数に渡す | |
// すると、removefirstElementのなかで配列のデータ自体が変化する | |
console.log(array); // [2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment