<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js function 实参、形参、绑定关系</title>
</head>
<body>
<script>
function changeStr(str) {
/*注意:此处再次更改了形参 str 的绑定关系。
这次绑定了全局变量 str 替换后的新的字符串。
所以此函数,并未实现改变全局变量的功能。*/
str = str.replace("open", "close");
console.log("str2:", str);
}
function updateArr(arr) {
/*更新数组。*/
arr.push(3, 4);
}
// 1.str 是不可变类型。
let str = "open";
console.log("str1:", str);
// 把全局变量 str 传给函数。
// 此时函数的形参绑定了全局变量 str。
changeStr(str);
console.log("str3:", str);
// str1: open
// str2: close
// str3: open
// 2.arr 是可变类型。
console.log("\n2.");
let arr1 = [1, 2];
updateArr(arr1);
console.log("arr1:", arr1);
// arr1: (4) [1, 2, 3, 4]
</script>
</body>
</html>
js function 实参、形参、绑定关系.html
于 2019-03-30 19:32:15 首次发布