Some small knowledge of the This keyword in JavaScript _javascript tips

Source: Internet
Author: User

JavaScript should be one of the most popular cross-platform languages that have been playing with some interesting things at the front end, and found that the language was not well mastered. A little trifles, so I want to take advantage of this time when there are free to add a little missing things.

The implicit binding of this

At first it was something I was confused about, and I didn't understand it when I first saw it. Then, in similar cases, the same problem can be solved in a similar way. Then try to clarify this knowledge, easy to find.

This is a design error in a JavaScript language, but it seems that the error is unavoidable, the function is an object, the array is an object, and so on. Citing the example in the Javascript:the good Parts

Copy Code code as follows:

function Add (a,b) {return a+b}
var sum = Add (3,4);
Console.log (sum); sum = 7

At this point the result of sum is 7.

Copy Code code as follows:

> typeof add
> ' Number '

As you can see here, the type of add is numeric.

When calling a function in this mode, this is bound to a global variable.
That is, in the current environment, we can call this

Copy Code code as follows:

This.add (3,4)

This is the implicit binding of this, and this is bound in a different way.
Copy Code code as follows:

var hello = function () {
Return "Hello," + this.name;
};
Name = ' this ';
Console.log (hello ());

Then we'll get hello,this. And when
Copy Code code as follows:

var hello = function () {
Return "Hello," + this.name;
};
var user = {
Hello:hello,
Name: ' Phodal ',

};
Console.log (User.hello ());


Then hello in user points to the Hello function, which, in our understanding, is not possible, so it is a bug.

If we define a variable in this method and assign it a value of this, then the internal function can access this by that variable.

var that = this

So when things get a little bit more complicated, we need to use:

Copy Code code as follows:

VAT that = this;

Tips

The scope of a 1.this variable is always determined by its closest enclosing function.
2. Use a local variable (such as Me,self,that) to make the this binding available internally.

A simple example:

Copy Code code as follows:

var M = function () {
THIS.name = "M";
};

var MM = function () {
z = new M ();
THIS.name = "MM";

Z.printname = function () {
Console.log (this.name);
};
return Z.printname ();
};

var mm = new mm;


This point is the M function, and by MM itself. If we remove this from the M, then the return is a undefined. So we create an alias for the current this scope, such as that or self, and so on:
Copy Code code as follows:

var MM = function () {
z = new M ();
THIS.name = "MM";
var self = this;
Z.printname = function () {
Console.log (Self.name);
};
return Z.printname ();
};

So you can return a mm. In addition, the bind method of the callback function can be used in ES5.

Copy Code code as follows:

var MM = function () {
z = new M ();
THIS.name = "MM";
Z.printname = function () {
Console.log (this.name);
}.bind (this);
return Z.printname ();
};

Bind can bind a method to the receiver.

Other

Another hello,world.

Encountered print (' Hello ') (' World ') in an accidental opportunity, and then output ' Hello, world '.

The so-called higher-order function, which seems to be very useful, is interested in looking at the next article.

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.