Method calls in JavaScript detail _javascript tips

Source: Internet
Author: User

In JavaScript, if a function belongs to an object, then the behavior through the object to access the function is called a method call. Unlike a normal function call, when a method call is made, the this reference in the function changes-this will refer to the object used to invoke the function (the object will become the invocation context of the method call):


Copy Code code as follows:

var x = 99;
var sample = {
X:1,
Act:function (a) {
This.x = a*a;//assign value to sample ' s X, not global object ' x.
}
}
Sample.act (6);
Console.log (sample.x);//36
Console.log (x);//99


As with accessing the property in an object, in addition to using the dot operator, JavaScript can be invoked by using the bracket operator:


Copy Code code as follows:

Other ways to Invoke method
sample["Act"] (7);
Console.log (sample.x);//49


A more interesting behavior for function in JavaScript is that you can embed function (closure) in function. In the case of a method call, if there is an embedded function in the method function, the code in the embedded function can access the external variable value:


Copy Code code as follows:

Nested function can access variable outside of it.
var y = 88;
var sample2 = {
Y:1,
Act2:function (a) {
This.y = inner ();
function inner () {
return a*a;
}
}
}
Sample2.act2 (8);
Console.log (SAMPLE2.Y);//64
Console.log (y);//88


However, contrary to intuition, code in an embedded function cannot inherit this from the outside, that is, in an embedded function, this refers not to the object calling the method, but to the global object:


Copy Code code as follows:

Nested function does not inherit ' this '. The ' this ' in nested function is global object
var sample3 = {
Act3:function () {
Inner ();
function inner () {
Console.log (this);//window object
}
}
}
Sample3.act3 ();


If you do need to access an object that invokes a method in an embedded function, you can save the this value in an external function to a variable:


Copy Code code as follows:

Pass ' this ' to nested function
var sample4 = {
Act4:function () {
var self = this;
Inner ();
function inner () {
Console.log (self);//object {act4=function ()}
}
}
}
Sample4.act4 ();

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.