JS four function call patterns

Source: Internet
Author: User

Look at the JS language essence, still slowly gnawing, gnawing to some before the thought of before the blog on the record it. Extra, the studio man sometimes too damn moral integrity!! You have no moral integrity even, in front of my face, this is what Ghost (collapse), do not say these useless, go to the point.

Calling a function pauses the execution of the current function, passing control and parameters to the new function. So what are the parameters of the new function, which involves the active object mentioned in the previous closure, and each function has two this,arguments, in addition to the formal parameters defined at the time of declaration. This is a very important parameter, and its value depends on the mode of the call. In JS in a total of 4 call Mode: Method call mode, function call mode, constructor call mode, apply call mode.

Speaking of which, how to invoke a function, which is followed by the expression of a pair of parentheses (), the parentheses can have one or more parameters. When the number of actual arguments does not match the number of formal parameters, a run-time error is not caused. If the argument is too large, the out-of-section is ignored, and if the argument is too small, it is replaced by undefined. No type checking is performed on the arguments passed in, and any type of value can be passed into the function. OK, the digression is also complete, tell me about four modes.

1. Method invocation Mode

When a function is saved as a property of an object, we call it a method. when a method is called, this is bound to the object . If the invocation expression contains an action that extracts a property (that is, contains a. or []), then it is invoked as a method. Because this is bound to this object, the method can use this to access the object it belongs to, so it can take values from the object or modify it. This binding to the image occurs at the time of invocation, and the method of obtaining the context of the owning object is called a public method.

2. Function call mode

When a function is not a property of an object, it is called as a function.

such as var sum = Add (3,5);

When this mode invokes a function, thisis bound to the global object . This design pattern causes the internal function to be called, this is the this ideal should be bound to the external function of this, but unfortunately, its this is equal to the global window. This pattern causes the method not to use intrinsic functions to help work, and cannot share the method's access to the object. To say that the problem is still to be solved, then how to solve it. Is that the method (the external function) defines a variable and assigns it to this, which is that = this, then the intrinsic function can access the This of the external function through that. is also a popular good way.

3. Constructor invocation mode

This invocation pattern is related to the prototype, and the prototype is not verbose here. If you are in front of a function with a new call, you will create a thought object that is connected to the prototype member of the function, and this will be bound to the new object . constructor function, which is created to provide a function of the new call.

4.apply Call mode

The Apply method takes two arguments to the calling function, one for the value to bind to this, and one for the parameter array. Array essence is to change a method or the scope of the image, change its this value, and then pass parameters to it. If the first parameter is passed null/undefined, this is the global, equal to window.

  

JS four function call patterns

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.