call()&apply() $.extend() in jquery

1. Definitions

call method 
syntax: call([thisObj[,arg1[,arg2[,[,..argN]]]]]) 
Definition: call a method of an object and replace the current object with another object 
Description: The call method can be used to Call a method in place of another object. The call method changes the object context of a function from the initial context to the new object specified by thisObj. 
If no thisObj parameter is provided, the Global object is used as thisObj.

Apply method 
syntax: apply([thisObj[,argArray]]) 
Definition: Apply a method of an object, replacing the current object with another object. 
Remarks: Raises a TypeError if argArray is not a valid array or arguments object. 
If neither argArray nor thisObj are provided, the Global object will be used as thisObj and no arguments can be passed.

2. Common examples

function add(a,b)  
{  
    alert(a+b);  
}  
function sub(a,b)  
{  
   alert(a-b);  
}  

add.call(sub,3,1);

Use add to replace sub, add.call(sub,3,1) == add(3,1), so the running result is: alert(4); // Note: The function in js is actually an object, and the function name is A reference to the Function object.

function Animal(){    
    this.name = "Animal";    
    this.showName = function(){    
        alert(this.name);    
    }    
}    

function Cat(){    
    this.name = "Cat";    
}    

var animal = new Animal();    
var cat = new Cat();    

//通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用了。    
//输入结果为"Cat"    
animal.showName.call(cat,",");    
//animal.showName.apply(cat,[]);  

Put the animal's method on cat and execute it. It turns out that cat has no showName() method. Now we put the animal's showName() method on cat to execute, so this.name should be Cat.

3. Implement inheritance

function Animal(name){      
    this.name = name;      
    this.showName = function(){      
        alert(this.name);      
    }      
}      

function Cat(name){    
    Animal.call(this, name);    
}          
var cat = new Cat("Black Cat");     
cat.showName();
  • Animal.call(this) means to use the Animal object instead of the this object, then don't Cat have all the properties and methods of Animal, and the Cat object can directly call the methods and properties of Animal.

4. Multiple inheritance

function Class10()  
{  
    this.showSub = function(a,b)  
    {  
        alert(a-b);  
    }  
}  

function Class11()  
{  
    this.showAdd = function(a,b)  
    {  
        alert(a+b);  
    }  
}  

function Class2()  
{  
    Class10.call(this);  
    Class11.call(this);  
 }  

Of course, there are other methods of js inheritance, such as using prototype chain, which is not within the scope of this article, just to explain the usage of call here. Speaking of call, of course there is also apply. These two methods basically mean the same thing. The difference is that the second parameter of call can be of any type, while the second parameter of apply must be an array, or arguments 
and callee . , caller.

$.extend(boolean,dst,src1,src2): merge src1,src2... into the dst object

boolean is deep merge; dst target element, can be omitted; 

Guess you like

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