1. This in the global scope
In strict mode, in the global scope, this points to the Window object
"Use strict";
Console.log ("strict mode");
Console.log ("This in global scope");
Console.log ("this.document = = Document", this.document = = document);
Console.log ("This = = Window", this = = window);
THIS.A = 9804;
Console.log (' THIS.A = = window.a=== ', WINDOW.A)
Console.log ("strict mode");
Console.log ("This in global scope");
Console.log ("this.document = = Document", this.document = = document);
Console.log ("This = = Window", this = = window);
THIS.A = 9804;
Console.log (' THIS.A = = window.a=== ', WINDOW.A);
2. This in a function in the global scope
In strict mode, this is equal to undefined in this function
"Use strict";
Console.log ("strict mode");
Console.log (' This ' in a function in the global scope);
Function F1 () {
console.log (this);
}
function F2 () {
function f3 () {
console.log (this);
}
F3 ();
}
F1 ();
F2 ();
3. This in the function (method) of the object
In strict mode, this in the function of an object points to an instance of an object calling a function
"Use strict";
Console.log ("strict mode");
Console.log ("This" in a function of an object);
var o = new Object ();
O.A = ' o.a ';
O.f5 = function () {return
this.a;
}
Console.log (O.f5 ());
4. This of the constructor function
In strict mode, this in the constructor points to the object instance created by the constructor.
"Use strict";
Console.log ("strict mode");
Console.log ("This" in the constructor);
function Constru () {
this.a = ' constru.a ';
THIS.F2 = function () {
console.log (this.b);
Return THIS.A
}
}
var O2 = new Constru ();
o2.b = ' o2.b ';
Console.log (O2.f2 ());
5. This in the event handler function
In strict mode, in the event handler function, this points to the target object that triggered the event.
"Use strict";
function Blue_it (e) {
if (this = = E.target) {
this.style.backgroundColor = ' #00f ';
}
}
var elements = document.getelementsbytagname (' * ');
for (var i=0; I<elements.length i++) {
Elements[i].onclick = blue_it;
}
The purpose of this code is to make the clicked element background color blue
6. This in the inline event handler function
In strict mode, in the inline event handler, there are two things:
<button onclick= "Alert ((function () {' Use strict '; return this}) ());" >
inline Event handling 1
</button>
<!--warning window characters for undefined-->
<button onclick= "' Use Strict '; Alert (This.tagName.toLowerCase ()); " >
Inline Event handling 2
</button>
<!--the characters in the Warning window are button-->
Resources
MDN https://developer.mozilla.org ...
Extension information
Nanyi Web log > JavaScript Strict mode detailed Http://www.ruanyifeng.com/blo ...
Rookie Tutorials > JavaScript Strict mode http://www.runoob.com/js/js-s ...