This content originally appeared on DEV Community and was authored by PUSHAN VERMA
this behaves in two different modes i.e Strict and Non -Strict.
(Strict is basically used in react)
_this1 and this2 are in node environment _
this1.js
console.log(this);
// 👉ans ->(returns window object (just like global object))
// Window
// index.html:42 Live reload enabled.
function f(){
console.log(this);
}
// 👉ans ->{}
// let obj1={
// name :'Pushan',
// f:function f(){
// console.log(this);
// }
// }
// obj1.f();
// 👉ans ->{name: 'Pushan', f: ƒ}
let obj2={
name:'Pushan',
f:function f(){
function g(){
console.log(this);
}
g()
}
}
obj2.f();
// ans -> window object
this2.js
'use strict'
console.log(this);
//👉 ans ->{}
function f(){
console.log(this)
}
f();
//👉 ans -> undefined
let obj={
name:"Pushan",
f:function(){
console.log(this)
}
}
obj.f();
// ans -> { name: 'Pushan', f: [Function: f] }
let obj2={
name:"Pushan",
f:function f(){
function g(){
console.log(this);
}
g()
}
}
obj2.f()
//👉 ans ->undefined
_this3 and this4 are in Browser Environment _
index.html
<!DOCTYPE html>
Document
<div class="highlight"><pre class="highlight plaintext"><code></script>
</code></pre></div>
<p></head><br>
<body></p>
<p></body><br>
</html></p>
<p><strong>this3.js</strong></p>
<p>console.log(this);</p>
<p>// 👉ans ->(returns window object (just like global object))<br>
// Window<br>
// index.html:42 Live reload enabled.</p>
<p>function f(){<br>
console.log(this);<br>
}</p>
<p>// 👉ans ->{}</p>
<p>let obj1={<br>
name :'Pushan',<br>
f:function f(){<br>
console.log(this);<br>
}<br>
} <br>
obj1.f();</p>
<p>// 👉ans ->{name: 'Pushan', f: ƒ}</p>
<p>let obj2={<br>
name:'Pushan',<br>
f:function f(){<br>
function g(){<br>
console.log(this);<br>
}<br>
g()<br>
}<br>
}</p>
<p>obj2.f();</p>
<p>// ans -> window object</p>
<p><strong>this4.js</strong></p>
<p><strong>index1.html</strong><br>
<!DOCTYPE html><br>
<html lang="en"><br>
<head><br>
<meta charset="UTF-8"><br>
<meta http-equiv="X-UA-Compatible" content="IE=edge"><br>
<meta name="viewport" content="width=device-width, initial-scale=1.0"><br>
<title>Document</title><br>
<script src="this4.js"></p>
<div class="highlight"><pre class="highlight plaintext"><code></script>
</code></pre></div>
<p></head><br>
<body></p>
<p></body><br>
</html></p>
<p><strong>this4.js</strong></p>
<p>'use strict';</p>
<p>console.log(this);</p>
<p>// 👉ans ->(returns window object (just like global object))<br>
// Window<br>
// index.html:42 Live reload enabled.</p>
<p>function f(){<br>
console.log(this);<br>
}</p>
<p>// 👉ans ->undefined</p>
<p>// let obj1={<br>
// name :'Pushan',<br>
// f:function f(){<br>
// console.log(this);<br>
// }<br>
// } <br>
// obj1.f();</p>
<p>// 👉ans ->{name: 'Pushan', f: ƒ}</p>
<p>let obj2={<br>
name:'Pushan',<br>
f:function f(){<br>
function g(){<br>
console.log(this);<br>
}<br>
g()<br>
}<br>
}</p>
<p>obj2.f();</p>
<p>// ans -> window object </p>
<p>Link for handwritten notes:<br>
<a href="https://github.com/pushanverma/notes/blob/main/webd/OOPs%20in%20Js%20.pdf">https://github.com/pushanverma/notes/blob/main/webd/OOPs%20in%20Js%20.pdf</a></p>
This content originally appeared on DEV Community and was authored by PUSHAN VERMA
PUSHAN VERMA | Sciencx (2022-02-15T12:31:09+00:00) Introduction to OOPS in Javascript (this keyword). Retrieved from https://www.scien.cx/2022/02/15/introduction-to-oops-in-javascript-this-keyword/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.