-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis-in-global.js
More file actions
32 lines (22 loc) · 921 Bytes
/
this-in-global.js
File metadata and controls
32 lines (22 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// "use strict";
exports.name = "Samrood";
console.log(this); //module object, {name:'Samrood'} // even in strict mode
function logThisInStrictMode() {
"use strict";
console.log(this); //undefined
}
logThisInStrictMode();
function logThis() {
console.log(this); //global object
}
logThis();
// in browsers
// modules are always in strict mode. they always give undefined for thisArg for global scope and global functions
// for normal scripts not in strict mode:
// thisArg in global scope and in inside global functions is the window object.
// for normal scripts in strict mode:
// thisArg in global will still output the window object.
// for inside functions in global scope, we get undefined just like in nodejs
// of course this is when this is not manually bound or it's an object method. we are talking about
// console.log(this) //out in global scope
// functions like the logThis function above