密鑰概念:
undefined
,符號)使用逐個傳遞,並對複合類型(對象和數組)進行通過轉文。 null
運算符確定分配方法。 typeof
JavaScript中的參考點直接指向數據,而不是其他變量。 原始值是不可變的;複合值是可變的。重新分配複合值會創建一個新的參考。 > JavaScript的參考行為與帶有明確指針的語言明顯不同。 僅通過引用傳遞複合數據結構。 >
>
運算符確定值是通過值分配還是通過參考分配。
typeof
> by-Reference(化合物值):
let batman = 7; let superman = batman; //assign-by-value superman++; console.log(batman); //7 console.log(superman); //8
創建新的參考:
let flash = [8, 8, 8]; let quicksilver = flash; //assign-by-reference quicksilver.push(0); console.log(flash); //[8, 8, 8, 0] console.log(quicksilver); //[8, 8, 8, 0]
>函數中的引用:
let batman = 7; let superman = batman; //assign-by-value superman++; console.log(batman); //7 console.log(superman); //8
>修改函數中的原始化合物值:
let flash = [8, 8, 8]; let quicksilver = flash; //assign-by-reference quicksilver.push(0); console.log(flash); //[8, 8, 8, 0] console.log(quicksilver); //[8, 8, 8, 0]
創建淺副本:
let firestorm = [3, 6, 3]; let atom = firestorm; //assign-by-reference atom = [9, 0, 9]; //value is reassigned (creates new reference) console.log(firestorm); //[3, 6, 3] console.log(atom); //[9, 0, 9]
通過參考(使用對象)分配原語:
>
>let magneto = [8, 4, 8]; (function(x) { //IIFE x.push(99); x = [1, 4, 1]; //reassign variable (creates new reference) x.push(88); })(magneto); console.log(magneto); //[8, 4, 8, 99]
結論:
(為簡潔而省略了FAQ部分,但可以根據需要重新添加。)>
以上是快速提示:JavaScript參考如何工作的詳細內容。更多資訊請關注PHP中文網其他相關文章!