JavaScript function calls

Source: Internet
Author: User

1. Calling a function pauses the execution of the current function, passing control and parameters to the new function.

2. In addition to the formal parameters defined at the time of declaration, each function has two additional parameters: this and arguments.

1. This is important in object-oriented programming, and its value depends on the mode of the call.

3. There are 4 modes of invocation in JS: Method invocation pattern, function call pattern, constructor invocation mode, and apply invocation pattern. These patterns differ on how to initialize the key parameter this.

1. Method invocation: When a function is a property of an object, it is called the method of the object, and when this method is called, the value of this is the object.

    

var obj = {    value:0,    Increment:function (inc) {        This.value + = typeof Inc = = = ' number '? Inc:1;    }} Obj.increment (); Console.log (obj.value);  1obj.increment (2); Console.log (Obj.value); 3

This is called a public method when the binding to an object occurs at the time of invocation, and the method through this can obtain the context of the object to which they belong.

2. Function call: The function is not inside the object, it is not a property of the object, it is called as a function. When a function is called this way, this points to the Global (window). But this is a JS design error: If the language design is correct, when the internal function is called, this should still be bound to the external function of this variable. The consequence of this design error is that the method cannot use an intrinsic function to help it work because the this of the intrinsic function is bound to the wrong value, so the method cannot share access to the object. Fortunately, there is an easy solution: If the method defines a variable and assigns it to this, then the intrinsic function can access this by using this variable, which, by convention, is named that, which is the famous var that = this;

    

 var  obj = {value:  1,  double : function   () { var  that = this ; //         var  helper = function   () {That.value  = that.value * 2;        };    Helper (); }}obj.  double    //  2  

3. Constructor invocation: JS is a prototype-based language that is classless (class) and that objects can inherit properties directly from other objects. Most languages today are class-based languages, and although prototype inheritance has a strong expressiveness, it deviates from mainstream usage and is not widely understood. In order to be compatible with the language-based authoring style, JavaScript provides a set of object-building grammars based on similar language: If you precede a function with the new keyword to invoke it, a new instance object that hides the prototype prototype object linked to the function is created. This will be bound to the new instance object. Note that the new prefix also alters the behavior of the return statement. var obj = new Base (); What does the new operator do? In fact, it's very simple, it's done three tiny jobs:

1. var obj = {};//Creates an empty object

2. obj.__proto__ = base.prototype;//points The __proto__ of the empty object to the prototype of the constructor Base ()

3. Base.call (obj); Point the this pointer of the base function object to obj, and then call the base function

  

 var  F = function   (String) { this . Status = string;}; F.prototype.get  = function   () { Span style= "color: #0000ff;" >return  this  .status;};  var  f = new  F ("New object" ); /*      1. Created an empty object var f = {}; 2. The __proto__ of the F object points to prototype 3 of F. The This in F points to the F object and calls the F () function  */ console.log ( F.get ());  //  "new Object"  

4. Apply call: Because JS is a functional object-oriented programming language, functions have methods. the Apply method of the function can construct a parameter array and use it to invoke the function , and also allow us to select the value of this. The Apply method consists of two parameters: the first parameter sets the value bound to this, and the second argument is an array that contains the function arguments.

  

varArray = [5, 4];varAdd =function(A, b) {returnA +b;};varsum = add.apply ({}, array);//9/*The apply method invokes the Add () function and passes the elements in array arrays as arguments to add (). */varF =function(string) { This. Status =string;}; F.prototype.get=function() {    return  This. Status;};varobj ={status:' OK '};varStatus = F.prototype.get.apply (obj);//"OK"/*Call the F.prototype.get () method to bind the this value in this method to obj*/

JavaScript function calls

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.