1、作用域和值类型引用类型的传递

作用域
var num1 = 11;
var num2 = 22;
function fn(num,num1){
    //num,num1会计作用域中变量,分别传入时候只为11,22
  num = 100;
  num1 = 100;
  num2 = 100;        //没有用var声明,不是块作用域中变量,那么修改的就是全局的num2
  console.log(num)        //100
  console.log(num1)        //100
  console.log(num2)        //100
}
    fn(num1,num2)
  console.log(num1)        //11
  console.log(num2)        //100
  console.log(num)        //undefined报错
值类型、引用类型的传递
function Person(name,age,salary){
  this.name = name;
  this.age = age;
  this.salary = salary;
}

function f1(person){
//执行f1(p)的时候person的地址和p的地址指向同一个内存地址,因此修改person.name会修改p.name
  person.name = "ls"        
  person = new Person('aa',18,10)        //此时重新将person指向另外一个引用地址,所以跟p无关,不修改p值
}

var p = new Person('zs',18,10000)
console.log(p.name)        //zs
f1(p)
console.log(p.name) //ls

results matching ""

    No results matching ""