Home  >  Article  >  Web Front-end  >  Detailed analysis of the use of apply method in js_javascript skills

Detailed analysis of the use of apply method in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:17:45981browse

1. The general approach to object inheritance is to copy: Object.extend
The implementation of prototype.js is:

Copy the code The code is as follows:

Object.extend = function(destination, source) {
for (property in source) {
destination[property ] = source[property];
}
return destination;
}

In addition, there is another method, which is: Function.apply (of course using Function. You can also call)

The apply method can hijack the method of another object and inherit the properties of another object

Function.apply(obj, args) method can receive two parameters

obj: This object will replace this object in the Function class

args: This is an array, which will be passed as parameters to Function (args-->arguments)

apply demonstration code is as follows:

Copy code The code is as follows: