S1: Dynamic method invocation: Call & Apply

Source: Internet
Author: User

JS in the execution of the function of two ways: one by calling the operator ' () ', and the second by invoking call or apply to execute dynamically.

The This object is specified in a dynamic method call

In development we often need to invoke the method of object A in object B, this time using apply () and call (), their first parameter is used to specify the This object, if NULL, the default host object is passed in.

Second, the stack is visible and modified

function Fun1 (v1) {var v1 = 100;} function fun2 (name) {fun1.apply (this,arguments); alert (name);} The incoming parameter has not been modified and still pops up ' myName ' fun2 (' myName ');

This is because, when fun1.apply () is called,arguments is replicated once: The value data is copied and the reference data is created . So the arguments in Fun1 and fun2, while seemingly identical, are actually two sets of data that are isolated. However, if you change the FUN1 to the following:

function fun1 () {//display Truealert (Arguments.callee.caller = = = fun2);}

So the outer function is still visible to the internally called function, even though arguments is in call () | | Apply () is isolated by copying, but the call stack is still visible to the called function, and the called function can still access the arguments on the stack, such as:

function fun1 (name) {Arguments.callee.caller.arguments[0] = 100;//alert (Arguments.callee.caller = = = fun2)}function FUN2 (name) {fun1.apply (this,arguments); alert (name);} Shows that the passed in parameters are changed to 100fun2 (' myName ');

In fun1, we accessed the parameters of the function on the stack through callee and caller, and modified the formal parameter name in fun2, but Fun2 did not know that the parameter had been modified, so this is extremely dangerous! The small pots of friends know that classes arguments and arrays are usually shared by a parent class-because they all have a length property that requires self-maintenance. Therefore, we can also apply the method in the array prototype to the arguments instance, for example:

[].slice.call (arguments,1);

But this also increases the risk on the call stack: not only can we modify the values of some of the parameters in arguments, but also we can modify the number of arguments incoming values. For example:

function Fun3 (name) {[].push.call (arguments.callee.caller.arguments,100);} function Fun4 (name) {fun3 ();  Show 2console.dir (arguments.length);} Fun4 (' MyName ');

S1: Dynamic method invocation: Call & Apply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.