基本类型
/*var a = 5;
var b = a;
b += 3;
alert(b); //8
alert(a); //5
*/
//对象和函数都是引用的关系
/var a = [1,2,3];
var b = a;
b.push(4);
alert(b); //1,2,3,4
alert(a); //1,2,3,4/
var a = [1,2,3];
var b = a;
b = [1,2,3,4];
alert(b); //1,2,3,4
alert(a); //1,2,3
/var obj = {
a : 10
};
var obj2 = obj;
obj2.a = 20;
alert(obj.a); //20/
var obj = {
a : 10
};
function copy(obj){ //浅拷贝
var newObj = {};
for(var attr in obj){
newObj[attr] = obj[attr];
}
return newObj;
}
var obj2 = copy(obj);
obj2.a = 20;
alert(obj.a); //10
var obj = {
a : { b : 10 }
};
/*function copy(obj){ //浅拷贝
var newObj = {};
for(var attr in obj){
newObj[attr] = obj[attr];
}
return newObj;
}*/
function deepCopy(obj){ //深拷贝
if(typeof obj != 'object'){
console.trace();
return obj;
}
var newObj = {};
for(var attr in obj){
newObj[attr] = deepCopy(obj[attr]);
}
return newObj;
}
var obj2 = deepCopy(obj);
obj2.a.b = 20;
alert(obj.a.b); //10
//递归:
//1.函数调用函数自身,执行递的动作
//2.最后一次判断一个终止条件,可以执行归的动作
function test(n){
if(n==1){
console.trace();
return 1;
}
return n*test(n-1);
}
alert(test(4));
文章最后发布于: 2018-02-12 23:29:21
————————————————
版权声明:本文为CSDN博主「桃根仙」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/taotaobaobei/article/details/79319442