js method borrowing

js An object borrows a method from another object. call and apply are basically the same, the difference lies in the usage, one is to pass in an array, the other is to pass in parameters directly.

function people(){};  
 var animal = {  
     name: "cc",  
     age:16,  
     say:function(m1,m2){  
         alert("他说:"+m1+"---"+m2);  
     }  
 };  
 animal.say.apply(people,["我开始说话","是啊"]);  
 animal.say.call(people,"我开始说话","是啊");  

Guess you like

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