基本类型和引用类型作为方法的参数:
基本类型作为方法的参数:
传递的是参数的值
在函数内部修改参数的值,不会改变外部变量
引用类型作为方法的参数
传递的是参数的引用
在函数内部修改参数的值,会影响外部变量
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
//将基本数据类型作为方法的参数,不会影响外部变量
function fn1(a) {
a = 100;
}
var x = 5;
fn1(x); //传递的是数值,按值传递
console.log(x); //x的值不变
//将引用数据类型作为的参数,会影响外部变量
function Student(name, age, salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
function fn2(stu) {
stu.name = "alice";
stu.age = 30;
stu.salary = 8000;
}
var stu1 = new Student("tom", 24, 6600);
fn2(stu1); //传递的是地址,按引用传递
console.log(stu1);
</script>
</head>
<body>
</body>
</html>