function () Use method _javascript technique in JS

Source: Internet
Author: User
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 continuing, 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 ();
Copy Code code as follows:

<script type= "Text/javascript" >

1, method invocation pattern
When a function is saved as an attribute of an object, we call it a method of the object, so this is bound to the object
var myobject={
Name: "MyObject",
value:0,
Increment:function (num) {
This.value + = typeof (num) = = ' number '? num:0;
return this;
} ,
Tostring:function () {
Return ' [Object: ' + this.name + ' {value: ' + This.value + '}] ';
}
}
Alert (myobject.increment) increment. toString ()); [Object:myobject {value:30}]


2, Function call mode
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, this should still be bound to the this variable on the external function when the internal function is called
var myobject={
Name: "MyObject",
value:0,
Increment:function (num) {
This.value + = typeof (num) = = ' number '? num:0;
return this;
} ,
Tostring:function () {
Return ' [Object: ' + this.name + ' {value: ' + This.value + '}] ';
},
Getinfo:function () {
var self=this;
Return (function () {
return this.tostring (); Inside anonymous function This points to the 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 this object through the variable
})();
}
}
Alert (myobject.increment) increment. toString ()); [Object:myobject {value:30}]


3, constructor call pattern
JavaScript is a language based on prototype inheritance, which means that objects can inherit properties directly from other objects, and that the language is classless.
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 (10);
alert (Myobject.value); 10

var otherobject = new MyObject ();
Otherobject.increment (20);
alert (Otherobject.value); 20

alert (myobject.target===myobject); Ture
Alert (MyObject.target.getInfo ()); [Object:myobject {value:10}]


4, Apply call mode
JavaScript is a function-oriented 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 ();
Alert (getinfo.apply (myobj)); [Object:myobject {value:0}], this points to myobj
Alert (getinfo.apply (window)); [Object Window], this point 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;
}
}
Func (1,2,3);

var i=0
for (i=0;i<=10;i++) {
document.write ("The number is" + i + "<br/>")
}

</script>

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.