js所有函数传参都是按值传递
1>传基本参数类型:被传递的值复制给函数形参
function addMath(num){
num += 10;
return num;
}
var counts = 20;
var results = addMath(counts);
alert("count="+counts+",result="+results);
couts未改变,不是按引用传递的
2>传递obj类型
function setName(obj){
obj.name = "Allen";
obj = new Dog();
obj.name = "Wangcai";
return obj;
}
var person = new Person();
var dog = setName(person);
alert("PersonName="+person.name+",DogName="+dog.name); // allen,wangcai
时间: 2024-11-10 09:57:40