{"id":1038,"date":"2024-05-26T08:16:33","date_gmt":"2024-05-26T08:16:33","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1038"},"modified":"2024-08-22T09:16:40","modified_gmt":"2024-08-22T09:16:40","slug":"understanding-this-in-javascript","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/understanding-this-in-javascript\/","title":{"rendered":"Understanding \u201cthis\u201d in JavaScript"},"content":{"rendered":"\n<p>Understaing &#8220;This&#8221; keyword in Javascript, Function in javascript have properties, Just like javascript Object have properties. And when function get execute, It get the<strong> this<\/strong> property<\/p>\n\n\n\n<p>Lets Take a closer look at \u201c<strong>this<\/strong>\u201d. this is a special character\/keyword in <strong><a href=\"https:\/\/www.appfinz.com\/blogs\/category\/javascript\/\">javascript<\/a><\/strong>, As we know working with this is little tricky because it can change value when you and you are not expecting<\/p>\n\n\n\n<p>Let see how we can track <strong>this in Javascript<\/strong><\/p>\n\n\n\n<p>First we check this in global context<\/p>\n\n\n\n<p>When we use this in global context that\u2019s mean its not written inside a function scope. It refers to window object in browsers because window object is the top level global object in browser<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application\/ecmascript&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;JavaScript&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">console.log(&quot;This is: &quot;, this);<\/pre><\/div>\n\n\n\n<p>It will return the window object in browser<\/p>\n\n\n\n<p>Whenever you create a global variable its added to a property of window object. Let see an example these both methods will work same<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application\/javascript&quot;,&quot;theme&quot;:&quot;3024-night&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;JavaScript&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">var myVar = 5;\nconsole.log(&quot;My var is: &quot;, this.myVar);\nconsole.log(&quot;My var is: &quot;, myVar);<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;JavaScript&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">var myVar = 5;\nfunction myFunction(){\n  myVar++;\n  console.log(&quot;My var is: &quot;, this.myVar);\n  console.log(&quot;My var is: &quot;, this);\n}\n\nmyFunction();<\/pre><\/div>\n\n\n\n<p>In that example the value of this will refer to the window object.<\/p>\n\n\n\n<p>Lets see the value of this when we use constructor function<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;JavaScript&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">function MyObject() {\n  var myVar = 300;\n  this.obj1 = 100;\n  this.obj2 = 200;\n  this.obj3 = function(){\n  return this.obj1 + this.obj2;\n }\n}<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Javascript&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">var newObject = new MyObject();<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/ecmascript&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;JavaScript&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">console.log(newObject.myVar); \/\/ undefined\nconsole.log(newObject.obj1); \/\/ 100\nconsole.log(newObject.obj2); \/\/ 200\nconsole.log(newObject.obj3()); \/\/ 300<\/pre><\/div>\n\n\n\n<p>Now in the first case we have created a variable <strong>myVar<\/strong>, Which is local to MyObject function. But in other cases this keyword refers to any of the instance we create with \u201cMyObject\u201d constructor function using new keyword. Basically instance is a Object which created by a <strong>Constructor Class<\/strong>.<\/p>\n\n\n\n<p>In \u201cMyObject\u201d function this is refers to object which is created from new keyword of constructor class. As i said myVar variable in local to MyObject function. So when we are accessing <strong>newObject.myVar<\/strong> it returns undefined because myVar can only be accessable through <strong>MyObject<\/strong> function means<strong> myVar scope <\/strong>is only for <strong>MyObject function<\/strong> scope and it cannot be access by any other object which we create new of <strong>MyObject.<\/strong> we have also used concept in two of our project <a href=\"https:\/\/appfinz.com\/tools\/jpg-image-compressor\/\">JPG Compressor<\/a> Online and <a href=\"https:\/\/appfinz.com\/tools\/youtube-vimeo-thumbnail-downloader\/\">Youtube Thumbnail Grabber<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understaing &#8220;This&#8221; keyword in Javascript, Function in javascript have properties, Just like javascript Object have<\/p>\n","protected":false},"author":1,"featured_media":1291,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[],"class_list":["post-1038","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1038"}],"version-history":[{"count":3,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1038\/revisions"}],"predecessor-version":[{"id":1293,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1038\/revisions\/1293"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/1291"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}