函数的四种调用方式

1.函数调用模式

function func(){
     console.log(this);  //window
}
func();

var func = function(){
     console.log(this);  //window
}

 

2.方法调用模式

var func = function(){
      console.log(this); //被调用的对象 o  
}
var o = {};
o.fn = func;
o.fn();

 

3.构造器调用模式

var Person = function(){
    this.name = "fucker";
    this.do = function(){
        console.log('how to fuck!')    
   };
};
var p = new Person();
p.do();

this指的是对象本身P,即当前对象

4.apply与call调用模式

apply中的this可以随意指定

func.apply(null); // this ->window
func.apply(o,[]);  //this -> o

func.call(null); // this ->window
func.call(o,'','');  //this -> o

 

 

posted on 2016-07-29 13:19  爱疯的小疯子  阅读(143)  评论(0编辑  收藏  举报

导航