Four ways to call a function

1. Function mode

function show(){

}

show()

var show=function(){

}

show()

2. Method Mode

var obj={

}

function show(){

}

obj.showFn=show;

obj.showFn();

3. Constructor pattern

function personFn(name){

  this.name=name;   

  this.showName=function(){

    console.log('name is'+this.name)

  } 

}

var person = new personFn ('yaozheng');

person.showName();

4. apply call mode

call apply to change the point of this

The difference between the knowledge point call and apply and how to use it

function Cat(){}

Cat.prototype={   

    food:"aaa",   

    showFood:function(){     alert(this.food)   }

}  

var blackCat=new Cat();

blackCat.showFood();

var whiteCat={food:'bbb'};

blackCat.showFood.apply(whiteCat);

the difference

obj.call(thisObj,[a,b,c])

obj.apply(thisObj,a,b,c)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325229810&siteId=291194637