Skip to content

Commit 47d9720

Browse files
committed
basics of this keyword
1 parent bf3f22e commit 47d9720

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Javascript/this-keyword/this-keyword-1.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Inside a function, the value of this depends on how the function is called.
55
6-
Case-1 - WITHOUT STRICT MODE - If a function is not in strict mode, and if the value of this is not set by the call, this will default to the global object, which is window in a browser.
6+
Case-1 - WITHOUT STRICT MODE - If a function is not in strict mode, and if the value of this is not set by the call, this will default to the global object, which is window in a browser or global in Node environment
77
88
Case-2 - In strict mode, however, the value of this remains at whatever it was set to when entering the execution context, 'this' will default to undefined. So, in strict mode, if this was not defined by the execution context, it remains undefined. The global object refers to 'undefined' in place of the windows object.
99
@@ -12,10 +12,10 @@ Case-2 - In strict mode, however, the value of this remains at whatever it was s
1212
// Case-1 - WITHOUT STRICT MODE - Since the following code is not in strict mode, and because the value of this is not set by the call, this will default to the global object, which is window in a browser, or global in node environment.
1313

1414
function f1() {
15-
return this
15+
return this === global
1616
}
1717

18-
// console.log(f1()); will print a very long list of Object of which the top-most property will bel 'global'
18+
console.log(f1()) // => true
1919

2020
// In a browser:
2121
f1() === window // true
@@ -27,10 +27,10 @@ f1() === global // true
2727

2828
function f2() {
2929
"use strict"
30-
return this
30+
return this === undefined
3131
}
3232

33-
f2() === undefined // true
33+
console.log(f2()) // true
3434

3535
/* Now the case when “this” Refers to a New Instance - HERE, `this` will refer to an object that inherits from `instance.prototype` . Meaning with exact JS syntax
3636

0 commit comments

Comments
 (0)