JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

8.7.3 The call() and apply() Methods

8.7.3 The call() and apply() Methods

call() and apply() allow you to indirectly invoke ( §8.2.4 ) a function as if it were a method of some other object. (We used the call() method in Example 6-4 to invoke Object.prototype.toString on an object whose class we wanted to determine, for example.) The first argument to both call() and apply() is the object on which the function is to be invoked; this argument is the invocation context and becomes the value of the this keyword within the body of the function. To invoke the function f() as a method of the object o (passing no arguments), you could use either call() or apply():; f.apply(o);

Either of the lines of code above are similar to the following (which assume that o does not already have a property named m):

o.m = f; // Make f a temporary method of o. o.m(); // Invoke it, passing no arguments. delete o.m; // Remove the temporary method.

In ECMAScript 5 strict mode the first argument to call()or apply()becomes the value of this, even if it is a primitive value or null or undefined. In ECMAScript 3 and non-strict mode, a value of null or undefined is replaced with the global object and a primitive value is replaced with the corresponding wrapper object.

Any arguments to call() after the first invocation context argument are the values that are passed to the function that is invoked. For example, to pass two numbers to the function f() and invoke it as if it were a method of the object o, you could use code like this:, 1, 2);

The apply() method is like the call() method, except that the arguments to be passed to the function are specified as an array:

f.apply(o, [1,2]);

If a function is defined to accept an arbitrary number of arguments, the apply()method allows you to invoke that function on the contents of an array of arbitrary length. For example, to find the largest number in an array of numbers, you could use the apply() method to pass the elements of the array to the Math.max() function:

var biggest = Math.max.apply(Math, array_of_numbers);

Note that apply()works with array-like objects as well as true arrays. In particular, you can invoke a function with the same arguments as the current function by passing the arguments array directly to apply(). The following code demonstrates:

// Replace the method named m of the object o with a version that logs

// messages before and after invoking the original method.

function trace(o, m) {

var original = o[m]; // Remember original method in the closure.

o[m] = function() { // Now define the new method.

console.log(new Date(), "Entering:", m); // Log message.

8.7 Function Properties, Methods, and Constructor | 187

var result = original.apply(this, arguments); // Invoke original. console.log(new Date(), "Exiting:", m); // Log message. return result; // Return result.

}; }

This trace() function is passed an object and a method name. It replaces the specified method with a new method that “wraps” additional functionality around the original method. This kind of dynamic alteration of existing methods is sometimes called “monkey-patching.”

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1