Home > Web Front-end > JS Tutorial > body text

A question about js this keyword_javascript skills

WBOY
Release: 2016-05-16 19:22:02
Original
1005 people have browsed it

So take it out and encourage everyone:
First run the following js code



My explanation of the code here:

foo.bar(); // Print foo
//1. alert implicitly calls the toString method and converts it into a string. The toString method is rewritten in foo, so it is foo

(foo.bar)();//print foo
//2. The execution here is the same as above

(foo.bar || null)();
/*
3. This is in IE6.0, Mozilla Firefox1.5.0. 7 and Opera 9.0 have different effects. IE and Opera are both objects. Mozilla's is foo
Let's not talk about these JS interpretation methods of || for the time being. This is different from this and in the bar method. The || operator is relevant. After passing | 🎜>
In other words, the scope of this here is no longer the foo object, but a global scope. Therefore alert(this) is equivalent to alert(window);
So it is object

BTW: It is possible that the || operator converts the execution of two expressions into a global scope comparison, so In IE and Opera, (foo.bar || null) returns a global reference, that is:
'bar': function () {
alert(this);//this here is already Global this, global this, is window
},
For details, I will add a piece of code at the end of the article to illustrate
*/

bar = foo.bar; bar();//What is returned is "[object Window]"
/*4.
This is the same object in IE6.0, Mozilla Firefox1.5.0.7 and Opera9.0, if you understand For the above execution, it is obviously easier to understand this sentence
The reason is the same as above. Here, the reference of foo.bar is given to a global variable bar, and the global variables are all subordinate to the reference of window. Look at the following code:
var a = 'never-online';
alert(this.a); //never-online
alert(window.a); //never-online
If you try bar = foo.bar ; bar();Change to the following code, maybe you can understand
foo.alert = foo.bar; foo.alert ();
The foo.alert here is still a reference to the foo object, so the foo object This in is still valid here and has not become a window object
because the bar attribute is obviously window, so although there is this in foo.bar, this is referenced as window
*/

Look again if we change this example to look like this:



You should understand the reason.

From this example (foo.bar || null)(); we can see that Mozilla's interpreter will be more "standard", while the interpretation methods of Opera and IE are different from Mozilla's. The || operator has different effects. As I said above, it is possible that the || operator converts the execution of two expressions into a global scope comparison, so in IE and Opera, (foo.bar || null) returns a global reference

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template