Use method of function () in JS

Source: Internet
Author: User

  By the nature of the function object, it is convenient to assign a function to a variable or to pass the function as a parameter, the following describes the use of the following function syntax

JavaScript functions are different from other languages, and each function is maintained and run as an object. By the nature of the function object, it is convenient to assign a function to a variable or to pass the function as a parameter. Before proceeding, let's take a look at the use syntax of the function:    The following is a reference fragment:  function Func1 (...) {...}   var func2=function (...) {...};   var func3=function func4 (...) {...};   var func5=new function ();  code as follows: <script type= "Text/javascript" >   //1, method invocation mode  //When a letter When a number is saved as an attribute of an object, we call it a method of the object, then this is bound to the object   VAR myobject={  name: "MyObject",  value:0,  Increm Ent:function (num) {  this.value + = typeof (num) = = ' number '? num:0;  return this; },  toString: function () {  return ' [Object: ' + this.name + ' {value: ' + This.value + '}] '; } }  alert (myobject.in Crement (increment). toString ()); [Object:myobject {value:30}]     //2, Function call pattern  //When a function is not a function of an object, it is called as a function, this is bound to the global object. This is a mistake in language design. If the language is designed correctly, when the internal function is called, this should still be bound to the this variable on the external function   var myobject={  name: "MyObject",  value:0,  Increment : function (num) {  this.value + = typeof (num) = = ' number '? num:0;  return this; },  tostring:fu Nction () {  return ' [Object: ' + this.name + ' {value: ' + This.value + '}] '; },  getinfo:function () {  var self=this;  return (function () { //return this.tostring ()//Inside anonymous function This points to global object window, Output [object window]   return self.tostring (); Defines a variable selft and assigns it a value of this, then the intrinsic function accesses to the object's this } (); } }  alert (10). Increment (). toString ()); [Object:myobject {value:30}]     //3, constructor invocation mode  //JavaScript is a language based on prototype inheritance, which means that objects can inherit properties directly from other objects, which There are no categories of languages.  //If a function is called with new on the front, a new object is created that hides the prototype member connected to the function, and this will be bound to an instance of the constructor.   function MyObject (name) {  this.name = name | | ' MyObject ';  this.value=0;  this.increment = function (num) {  this.value + = typeof (num) = = ' number '? Num: 0; };  this.tostring = function () {  return ' [Object: ' + thiS.name + ' {value: ' + This.value + '}] '; }  this.target = this; }    MyObject.prototype.getInfo = function () {  return this.tostring (); }   // Also creates a Myobject.prototype object, MyObject inherits all of Myobject.prototype's properties, this is bound to the MyObject instance     var myObject = new MyObject ();  myobject.increment (a);  alert (myobject.value); 10    var otherobject = new MyObject ();  otherobject.increment ();  alert (otherobject.value); 20    alert (myobject.target===myobject); ture  alert (MyObject.target.getInfo ()); [Object:myobject {value:10}]     //4, Apply invocation mode  //JavaScript is a functional object-oriented programming language, so functions can have methods. The Apply method of a function, as if the object owns this method, and this point points to the object.  /apply receives two parameters, the first is the object to bind (this is the object), and the second is the parameter array .  function MyObject (name) {  this.name = name | | ' MyObject ';  this.value = 0;  this.increment = function (num) {  this.value + = typeof (num) = = ' number '? num : 0; };  This.tostring=function () {  return ' [Object: ' +this.name+ ' {value: ' +this.value+ '}] '; }  this.target= this; }  function GetInfo () {  return this.tostring (); }  var myobj = new MyObject ();  Aler T (getinfo.apply (myobj)); [Object:myobject {value:0}], this points to myobj  alert (getinfo.apply (window)); [Object Window], this points to window     //For and while  function func (a,b) {  alert (a);//1  alert (b); 2    for (var i=0;i<arguments.length;i++) {  alert (arguments[i);//1, 2, 1, 2, 3 }    var i=0;  while (i<arguments.length) {  alert (arguments[i]);//1, 2, 1, 2, 3  i=i+1; } ,}&nbs P Func (1,2,3);    var i=0  for (i=0;i<=10;i++) { -document.write ("The number is" + i + "<br/>" )  }    </script>   
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.