密钥概念:
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中文网其他相关文章!