Javascript: Functions (1)

Source: Internet
Author: User

Function object

A function is an object. The object generated by the object literal is linked to object. prototype. Function objects are linked to function. prototype. Each function is created with the hidden attributes of two attachments: the context of the function and the Code for implementing the function behavior.

Function literal

Function objects can be created using the function literal.

VaR add = function (a, B) {return a + B ;};

Call Invocation

In addition to the formal parameters defined during declaration, each function receives two additional parameters: This and arguments.

This is very important in Object-Oriented Programming. Its value depends on the call mode. In JS, there are four call modes, which differ in the initialization parameter This.

1. method call mode

2. function call mode

3. constructor call mode

4. Apply call mode

Method call mode the mehtodd invocation pattern

This is bound to an object when a function is saved as an object property and called.

The method that uses this to obtain the context of the object to which they belong is calledPublic method.

var myObject = {
value : 0,
increment:function(inc)
{
this.value += (inc);
}
};
myObject.increment(3);
myObject.increment(3);
alert(myObject.value);//result is 6.

Function call mode the function invocation pattern

In function call mode, this is bound to a global object. This is an error in Javascript language design.

See the following code:

var g='globle';
var o=function()
{
var g= 'self';
alert(this.g);
}();
//result is 'globle'. it is not correct.

You can use the following methods to avoid

var g='globle';
var o=function()
{
var that = this;
that.g='self';
alert(this.g);
}();
//result is 'self', it is correct.

Consultant invocation pattern

Javascript is a prototype inherited language, so objects can directly inherit attributes from other objects. The language is classless.

If a function is called with new in front of it, a new object hidden from the prototype member of the function will be created, and this will be bound to the new object.

This method is not recommended.

var Quo=function(string)
{
this.status =string;
};
Quo.prototype.get_status = function(){return this.status;};
var myQuo = new Quo("Confused");
alert(myQuo.get_status());
//new could not be lost.

Apply call mode the apply invocation pattern

Call a function using the apply/call method. The first parameter is the value of this we specified.

var  statusObject = {status : 'A-OK'};
var status = Quo.prototype.get_status.apply(statusObject);
//result is 'A-OK';the first param is "this" value.

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.