Common Methods for js function calling _ basic knowledge

Source: Internet
Author: User
Js function calls will be provided free of charge with two additional parameters: this and arguments. We know there should be four call methods. Next we will introduce them in detail for you, if you are interested, refer to the source javascript language. This is not the source code in the book.
Js function calls are free of charge. The extra parameters are this and arguments. Arguments is a parameter array. It is not a real array, but the length can be obtained using the. length method.
The call method in section 4 is as follows:
Method call mode
Function call mode
Constructor call mode
Apply call mode

Let's take a look at some instances for better understanding.
1: method call mode
Note that this points to myobject.

The Code is as follows:


/* Method call mode */
Var myobject = {
Value: 0,
Inc: function (){
Alert (this. value)
}
}
Myobject. inc ()


2: function call mode
Note that this points to window.

The Code is as follows:


/* Function call mode */
Var add = function (a, B ){
Alert (this) // this is bound to the window
Return a + B;
}
Var sum = add (3, 4 );
Alert (sum)


3: constructor call mode
We recommend that you discard this method in the essence of javascript. Because there is a better way. This document is not described here. Post the post on the next blog post.
A connection will be added here.

The Code is as follows:


/* Discard the constructor call mode */
Var quo = function (string ){
This. status = string;
}
Quo. prototype. get_status = function (){
Return this. status;
}
Var qq = new quo ("aaa ");
Alert (qq. get_status ());


4: apply call mode
= Let's look at a more useful apply instance. See the following code.

The Code is as follows:


/* Apply */
// Note that the above sum function is used
// With myobject
// The advantage of this call method is that it can point to the object that this points.
// The first parameter of apply is the object to which the this Pointer Points.
Var arr = [10, 20];
Var sum = add. apply (myobject, arr );
Alert (sum );


See the apply application. Bind is a function of binding time.

The Code is as follows:


Var bind = function (object, type, fn ){
If (object. attachEvent) {// IE browser
Object. attachEvent ("on" + type, (function (){
Return function (event ){
Window. event. cancelBubble = true; // stop time bubble
Object. attachEvent = [fn. apply (object)]; // ---- Here I want to talk about
// Use attachEvent in IE to add a time binding later.
// This does not point to the object itself. This. id in the function we bind cannot work normally.
// But if we use fn. apply (object)
// Here we can see that the first object of apply, that is, the point of this, is changed to the object, so this. id is changed
// Object. id can work properly.
}
}) (Object), false );
} Else if (object. addEventListener) {// other browsers
Object. addEventListener (type, function (event ){
Event. stopPropagation (); // stop time bubble
Fn. apply (this)
});
}
}
Bind (document. getElementById ("aaa"), "click", function () {alert (this. id )});

Related Article

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.