A simple understanding of call in Javascript and the ability to apply javascript to object orientation was later added. For compatibility, many strange things were made,
Function Animal () {this. name = "Animal"; this. showName = function () {alert (this. name) ;}} function Cat () {this. name = "Cat";} var animal = new Animal (); var cat = new Cat (); // use the call or apply method to convert the showName () that originally belongs to the Animal object () the method is handed over to the object cat for use. // The input result is "Cat" animal. showName. call (cat, ","); // animal. showName. apply (cat, []);
Therefore, we can see that call and apply occur to dynamically change this. When an object does not have a method, but other methods include, we can use call or apply to operate with other object methods.
The dom Node Selected through document. getElementsByTagName is an array similar to array. It cannot apply methods such as push and pop in Array. We can use:
Var domNodes = Array. prototype. slice. call (document. getElementsByTagName ("*"));
In this way, domNodes can apply all the methods in Array.