Skip to content

Latest commit

 

History

History
53 lines (44 loc) · 953 Bytes

函数的三种角色理解.md

File metadata and controls

53 lines (44 loc) · 953 Bytes

函数的三种角色

var num = 0;
function Fn(num) {
  arguments[0] = 100;
  this.num = 200;
  console.log(num);
  console.log(this.num);
  return num;
}

Fn.num = 400;
Fn.prototype.num = 300;
Fn.prototype.getNum = function () {
    console.log(this.num);
};

var f = new Fn(num);
console.log(f.num);
f.getNum();
Fn.prototype.getNum();
console.log(num);

var n = Fn(num);
console.log(n);
console.log(num);

解析思路如下:

var num = 0;
function Fn(num) {
  arguments[0] = 100;
  this.num = 200;
  console.log(num);//100
  console.log(this.num);//200
  return num;
}

Fn.num = 400;
Fn.prototype.num = 300;
Fn.prototype.getNum = function () {
    console.log(this.num);
};

var f = new Fn(num);
console.log(f.num);//200
f.getNum();//200,
Fn.prototype.getNum();//300
console.log(num);//0

var n = Fn(num);
console.log(n);//100,200,100
console.log(num);//200