JS function call pattern

Source: Internet
Author: User

1  , a function call   invokes a function that pauses the execution of the current function, passing control and parameters to the new function. In addition to the formal parameters that are defined when the function is declared, each function also accepts two additional arguments: this and arguments (arguments is not a true array, it has the length property, but it lacks all the methods of the array). Parameter this is very important in object-oriented programming, and its value depends on the mode of the call. In JS, there are four modes of invocation: Method invocation pattern, function call pattern, constructor invocation mode, and apply invocation mode. These patterns differ on how to initialize the key parameter this. The number of arguments depends on the number of parameters when the function is defined, and if the function is called, the parameter number mismatch of the passed shape participation function definition does not result in a run error, too many arguments are passed on the call, the parameters that are exceeded are ignored, if too little, the missing value is replaced by undefined.
2. Method invocation Mode
1 varMYOBJ = {2value:0,3Incrementfunction(val) {4                  This. Value + =typeofval = = = "Number"? Val:1;//This is a MyObj object5            }6      };7 myobj.increment ();8document.write (Myobj.value);//19Myobj.increment (2);Tendocument.write (Myobj.value);//3
   The method can be accessed using this, so it can take values from the object or modify the object. This binding to the object occurs at the time of the call. This "super" late binding allows the function to be reused for this height. The method through this to obtain the context of the object to which they belong is called a public method.

3. Function call mode

 1  var  name = "Zhangsan"; // AA variable is loaded onto the Window object  2  function   Test () { 3  function   Test1 () { 4  DOCUMENT.WR ITE (this . Name); // zhangsan,this is the Window object  5  };  6   Test1 ();  7   8
      test ();   

this is bound to the global object when the function is called in this mode. This is a mistake in language design. If the language is designed correctly, this should still be bound to the this variable of the external function when the intrinsic function is called. 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 a value of this, then the intrinsic function can access this by that variable, and by convention I give that variable a command for that

1 //Purpose: Use the Add method to add values2 //add a double method to MyObject. 3MyObject.Double=function(){4    varthat = This;//Workaround5       varHelper =function(){6That.value = Add (That.value,that.value);//Add method to add two numbers7       };8Helper ();//invoke helper as a function9 };Ten //calling double in method mode OneMyObject.Double(); Adocument.write (Myobject.getvalue ());//6, so the helper method can use this

4. constructor Invocation mode

When an object instance invokes a method in a prototype, the method can use this to access the properties or methods within its constructor.

1 //create a constructor named Quo that constructs an object with the Status property. 2 varQuo =function(str,name) {3     This. Status =str;4     This. Name =name;5 };6 //provides a public method named Get_status for all instances of quo. 7Quo.prototype.get_status =function(){8    return  This. status;//This can access the status9 };TenObject.prototype.get_name =function(){ One    return  This. Name;//can access the name A }; - //constructs a quo instance - varMyquo =NewQuo ("Confused", "Zhangsan"); the //Confused,document.write () and Document.writeln are the methods that JS writes to the client, Writeln is output in rows, but does not refer to the line break in the actual effect of the page, that is, the actual effect of the page is not wrapped.  - Document.writeln (Myquo.get_status ()); -document.write (Myquo.get_name ());//Zhangsan

5. Apply Call mode

The Apply method lets us construct a parameter array and use it to invoke the function. It also allows us to select the value of this.
The Apply method accepts two parameters, the first is the value that will be bound to this, and the second is an array of arguments.

1 var mystatus = {2    Status: ' A-ok '3}; 4 // Mystatus did not inherit from Ouo.prototype. But we can raise the Get_status method in Mystatus, although Mystatus does not have a method named Get_status 5var st = Quo.prototype.get_status.apply (Mystatus); // A-OK 6 var st1 = myQuo.get_status.apply (mystatus); // A-OK

JS function call pattern

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.