var | let | const | |
---|---|---|---|
作用域范围 | 函数 | 块 | 块 |
否覆盖声明 | true | err | err |
变量提升 | true | false | false |
再次赋值 | true | true | err |
javascript对象是引用传递,基本类型是按值传递
ps:javascript里没有引用传递其实是传递引用?
function changeStuff(a, b, c)
{
a = a * 10;
b.item = "changed";
c = {item: "changed"};
}
var num = 10;
var obj1 = {item: "unchanged"};
var obj2 = {item: "unchanged"};
changeStuff(num, obj1, obj2);
console.log(num);
console.log(obj1.item);
console.log(obj2.item);
console.log([1] === [1])//false
console.log([1] == [1])//false
- json 对象的拷贝函数
function copy(o) {
if (Object.prototype.toString.call(o) === '[object Array]') {
return o.map(item=>copy(item));
} else if (Object.prototype.toString.call(o) === '[object Object]') {
return Object.keys(o).reduce((result, key)=> {
result[key] = copy(o[key]);
return result;
}, {})
}
else {
return o;
}
}