{"id":318,"date":"2022-01-29T21:46:29","date_gmt":"2022-01-29T21:46:29","guid":{"rendered":"http:\/\/codebitshub.com\/?p=318"},"modified":"2022-02-04T21:22:09","modified_gmt":"2022-02-04T21:22:09","slug":"this-in-javascript","status":"publish","type":"post","link":"https:\/\/codebitshub.com\/this-in-javascript\/","title":{"rendered":"The Complete Beginner&#8217;s Guide to &#8216;this&#8217; in Javascript"},"content":{"rendered":"<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In order to comprehend the meaning of the keyword &#8216;<strong>this&#8217;<\/strong>\u00a0in javascript, you must first understand the meaning of the word <\/span><b>context<\/b><span style=\"font-weight: 400;\"> in the actual world.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">As you may know, the word <\/span><b>context<\/b><span style=\"font-weight: 400;\"> refers to the underlying concepts and facts which are required to comprehend an idea or statement.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">For example, if you take a look at a dictionary for the definition of the word <\/span><b>set<\/b><span style=\"font-weight: 400;\">, you will find that there is a huge list of different meanings of this word that depends on the situation under which the word is used.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">That situation is the <\/span><b>context<\/b><span style=\"font-weight: 400;\"> that will help you to grasp the full meaning of the word.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">But how is this related to the &#8216;<strong>this&#8217;<\/strong> keyword in javascript?\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Well, the meaning of &#8216;<strong>this&#8217;<\/strong> <\/span><span style=\"font-weight: 400;\">in javascript depends on the context of a function call (invocation).<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In javascript language, there are the following different function invocation types:<\/span><\/p>\n<ol style=\"text-align: justify;\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Function invocation<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Method invocation<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Constructor invocation<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Indirect invocation<\/span><\/li>\n<\/ol>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">For each invocation type, the &#8216;<strong>this&#8217;<\/strong> keyword behaves differently.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Furthermore, another parameter that affects the execution context is the usage of the <\/span><b>strict mode <\/b><span style=\"font-weight: 400;\">in javascript.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In this article, we are going to examine the meaning of the keyword &#8216;<strong>this&#8217; <\/strong>in javascript <\/span><span style=\"font-weight: 400;\">depending on the context a.k.a the function invocation type.<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Function_invocation\" >Function invocation<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#The_strict_mode_in_a_function_invocation\" >The strict mode in a function invocation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Method_invocation_implicit_binding\" >Method invocation (implicit binding)<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Common_misunderstandings_with_methods\" >Common misunderstandings with methods<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#%E2%80%98this_in_inner_function_of_an_object_method\" >&#8216;this&#8217; in inner function of an object method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Extracting_a_method_from_an_object\" >Extracting a method from an object<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Indirect_invocation_explicit_binding\" >Indirect invocation (explicit binding)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Constructor_invocation\" >Constructor invocation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Extra_bits\" >Extra bits<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Bound_functions\" >Bound functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Arrow_functions\" >Arrow functions<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Function_invocation\"><\/span><span style=\"font-weight: 400;\">Function invocation<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The term a function invocation refers to the execution of the code that the body of a function includes. An equivalent term of the function invocation is <\/span><b>the call of a function<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">As you may know, the javascript engine executes a code block through <strong>two<\/strong> phases.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The first phase is called <\/span><b>Creation Phase<\/b><span style=\"font-weight: 400;\"> and the second <\/span><b>Execution Phase<\/b><span style=\"font-weight: 400;\">.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">During the <\/span><b>Creation Phase,<\/b><span style=\"font-weight: 400;\"> the javascript engine creates a <\/span><b>Global Execution Context<\/b><span style=\"font-weight: 400;\"> and uses it to store the function declarations in the memory heap and variables. The initial values of each function and variable are equal to undefined.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">After the completion of the creation phase, the <\/span><b>Execution Phase<\/b><span style=\"font-weight: 400;\"> takes place. This is the moment that the code starts to run in the context that was formed in the <\/span><b>Creation Phase<\/b><span style=\"font-weight: 400;\">.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">So what does all this has to do with the &#8216;<strong>this&#8217; <\/strong>in javascript?\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The &#8216;<strong>this&#8217;<\/strong>\u00a0keyword is referring to the <\/span><b>Global Execution Context <\/b><span style=\"font-weight: 400;\">that was created during the <\/span><b>Creation Phase<\/b><span style=\"font-weight: 400;\">.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">When you execute the code in a browser, the Global Execution Context will refer to the <\/span><b>window object<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">On the other hand, if you execute the code in a <\/span><b>Node JS<\/b><span style=\"font-weight: 400;\"> environment the global context will be the <\/span><b>global object<\/b><span style=\"font-weight: 400;\"> instead of the <strong>window<\/strong> object.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example of code.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"mdqVzpE\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/mdqVzpE\"><br \/>\nUntitled<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">At the time the javascript engine calls the <strong>foo()<\/strong> function it sets <strong>this<\/strong> to the <strong>window<\/strong> context. <\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Similar to the above if you use the &#8216;<strong>this&#8217; <\/strong>keyword outside of any function scope it will equal the <strong>window<\/strong> object.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"The_strict_mode_in_a_function_invocation\"><\/span><span style=\"font-weight: 400;\">The strict mode in a function invocation<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Since ECMAScipt 5, the &#8216;<strong>strict mode<\/strong>&#8216; of javascript is supported which is a restricted variant of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Strict_mode\" target=\"_blank\" rel=\"noopener\">javascript<\/a><\/span><span style=\"font-weight: 400;\">. The strict mode has intentionally different semantics from normal code and what it does, is to provide stronger error checking and perform optimizations.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In order to use it, you have to add the directive &#8216;<strong>use strict&#8217;<\/strong> at the top of a function body. It is important to remember that once the &#8216;<strong>strict mode<\/strong>&#8216; is enabled it will affect the execution context and <\/span><b>this <\/b><span style=\"font-weight: 400;\">value will be undefined in a function invocation.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"VwreExv\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/VwreExv\"><br \/>\nStrict mode function invocation<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Additionally, when you enable the &#8216;<strong>strict mode<\/strong>&#8216; at the top of a function body, it will affect the scope for any nested function too.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"JjOGmvg\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/JjOGmvg\"><br \/>\nUntitled<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Finally, it is possible to use both &#8216;<strong>strict mode<\/strong>&#8216; and &#8216;<strong>non-strict mode<\/strong>&#8216; in different functions in the same javascript file.\u00a0<\/span><\/p>\n<p>Let&#8217;s see an example.<\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"WNXraYM\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/WNXraYM\"><br \/>\nUntitled<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Method_invocation_implicit_binding\"><\/span><span style=\"font-weight: 400;\">Method invocation (implicit binding)<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You can save a function as a property in an object too. This type of function is called a <\/span><b>method<\/b><span style=\"font-weight: 400;\">. In order to access a method, you will have to use a property accessor.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">A method invocation takes place when you access a method and add next to it an open parenthesis any arguments, and finally a close parenthesis.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The keyword &#8216;<strong>this&#8217; <\/strong>in this case will refer to the object that owns the method.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"YzEwJBW\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/YzEwJBW\"><br \/>\nMethod invocation<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non &lt;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The same is true in the cases that an object inherits a method from its prototype or the ECMAScript 2015 class syntax is used. In this case, the &#8216;<strong>this<\/strong>&#8216; keyword will refer to the object that owns the method which will be the object itself.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Common_misunderstandings_with_methods\"><\/span><span style=\"font-weight: 400;\">Common misunderstandings with methods<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In this section, I will mention two common errors that you possibly could face working with methods.<\/span><\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"%E2%80%98this_in_inner_function_of_an_object_method\"><\/span><span style=\"font-weight: 400;\">&#8216;this&#8217; in inner function of an object method<\/span><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"qBVbJvE\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/qBVbJvE\"><br \/>\nUntitled<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You may expect the &#8216;<strong>this<\/strong>&#8216; keyword to be equal to myObject but in this case, this is not true.\u00a0 Since the function <\/span><b>testThis<\/b><span style=\"font-weight: 400;\"> is not a method but a function that is declared inside the <strong>myObject<\/strong> object, its context will be the global context even if the logs function context is the <\/span><b>myObject <\/b><span style=\"font-weight: 400;\">object.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">If you want to change the context of the <\/span><b>testThis<\/b><span style=\"font-weight: 400;\"> function you will have to use one of the following ways that will bind the function to the desired context. You could use the <\/span><b>apply<\/b><span style=\"font-weight: 400;\"> method, the <\/span><b>call<\/b><span style=\"font-weight: 400;\"> method, or an <\/span><b>arrow<\/b><span style=\"font-weight: 400;\"> function instead.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"bGYEmJL\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/bGYEmJL\"><br \/>\nUntitled<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<h4 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Extracting_a_method_from_an_object\"><\/span><span style=\"font-weight: 400;\">Extracting a method from an object<\/span><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Another common misunderstanding is to extract a method from an object and assign it to a variable. The function context will be the global context in this case too.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"jOaWeop\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/jOaWeop\"><br \/>\nExtracting a method from an object<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Similar to the previous case, you can change the context of the <strong>testThis<\/strong> function if you use one of the <strong>call<\/strong> or <strong>apply<\/strong> methods.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"bGYEmPL\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/bGYEmPL\"><br \/>\nchange context with call method<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Indirect_invocation_explicit_binding\"><\/span><span style=\"font-weight: 400;\">Indirect invocation (explicit binding)<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">As you may know, Functions in javascript are <strong>first-class objects<\/strong>. The type of function object is <\/span><b>Function<\/b><span style=\"font-weight: 400;\">. Since it is an object, it inherits all the methods and properties from its prototype.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Function#Methods\">list<\/a> of methods that are inherited,<\/span><span style=\"font-weight: 400;\">\u00a0you can see that there are two methods that are related to the context the function will be called.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">These are the methods <\/span><b>apply <\/b><span style=\"font-weight: 400;\">and <\/span><b>call<\/b><span style=\"font-weight: 400;\">.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The method <\/span><b>apply<\/b><span style=\"font-weight: 400;\"> accepts as the first argument the context for the function invocation and as the second argument an array of arguments that are passed to the called function.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Likewise, the method <\/span><b>call<\/b><span style=\"font-weight: 400;\"> accepts as the first argument the context that will be used for the function invocation and a list of arguments that are passed to the called function.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">This way of a function invocation through the methods <\/span><b>apply<\/b><span style=\"font-weight: 400;\"> and <\/span><b>call<\/b><span style=\"font-weight: 400;\"> is called indirect invocation.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"qBVbQWO\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/qBVbQWO\"><br \/>\nindirect function invocation <\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You can use this kind of invocation when you need to call a function in a specific context.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Another useful use case could be to chain constructors in the same context .<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"XWzXyrL\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/XWzXyrL\"><br \/>\nchain constructors<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You can see more information about the <\/span><b>call<\/b><span style=\"font-weight: 400;\"> function <\/span><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Function\/call\"><span style=\"font-weight: 400;\">here.<\/span><\/a><\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Constructor_invocation\"><\/span><span style=\"font-weight: 400;\">Constructor invocation<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">One of the ways to create an Object in javascript is with the usage of a <a href=\"http:\/\/codebitshub.com\/javascript-prototypes\/#Constructor_method\">constructor<\/a> function<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The constructor will create a new object and initialize its methods and properties with any data are given in the arguments.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In order to use the function constructor method, you will have to use the <\/span><b>new<\/b><span style=\"font-weight: 400;\"> keyword.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In this case, the &#8216;<strong>this&#8217;<\/strong>\u00a0keyword will refer to the object when the object is created.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"qBVbJKW\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/qBVbJKW\"><br \/>\nUntitled<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The directive <\/span><b>new Foo(<\/b><strong>&#8216;<\/strong><b>Hello&#8217;)<\/b><span style=\"font-weight: 400;\"> makes a constructor invocation and passes the string &#8216;<\/span><b>Hello&#8217;<\/b><span style=\"font-weight: 400;\">\u00a0to the constructor. <\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Then the constructor function will create a new Object named myObject.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Inside the constructor function, the context is the <strong>Foo<\/strong> function object. As you can see in the logs after you access the property <\/span><b>hello<\/b><span style=\"font-weight: 400;\"> of the object <\/span><b>myObject<\/b><span style=\"font-weight: 400;\"> it will have the value &#8216;<\/span><b>Hello&#8217;.<\/b><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">What is happening is that the javascript engine creates an empty object (<strong>Foo<\/strong>) and makes it the context of the constructor method.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Next, it uses this empty object to populate any properties and methods and assigns them to the <strong>myObject<\/strong> object.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You can apply a similar logic using the class syntax. <\/span><span style=\"font-weight: 400;\">Let\u2019s see the above code with the class syntax.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"MWOKzpM\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/MWOKzpM\"><br \/>\nthis in Class<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><b>Note<\/b><span style=\"font-weight: 400;\">: it is considered good practice to check the context of an object in a function constructor. This will protect you from errors like forgetting the use of the new keyword.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In this case, the context would be the global object.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"jOaWQwd\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/jOaWQwd\"><br \/>\ncontext check<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Extra_bits\"><\/span><span style=\"font-weight: 400;\">Extra bits<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">I could not omit from this article to mention two important factors that are related to the usage of &#8216;<strong>this&#8217;<\/strong> in javascript. These are the bound functions and the arrow functions.<\/span><\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Bound_functions\"><\/span><span style=\"font-weight: 400;\">Bound functions<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">You can create a bound function with the usage of the <strong>bind<\/strong> method. The first argument of the bind method is the context that the function will use.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The bind method will create and return the bound function that will be executed in the predefined context.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"dyZGQVW\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/dyZGQVW\"><br \/>\nBound Functions<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\"><strong>Note<\/strong>: you can pass any arguments in the bound function as normally you would do.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Finally, I have to mention that the <\/span><b>bind<\/b><span style=\"font-weight: 400;\"> method creates a permanent context to the object and cannot be changed even with using the <strong>call\/apply<\/strong> methods.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">The only way to change an already bound context is by using the constructor function of the object with the <\/span><b>new<\/b><span style=\"font-weight: 400;\"> keyword.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"abVdQVN\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/abVdQVN\"><br \/>\nBound functions change context<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\no<\/p>\n<h3 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Arrow_functions\"><\/span><span style=\"font-weight: 400;\">Arrow functions<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Arrow functions are a compact alternative to normal functions. An arrow function resolves its context <strong>lexically<\/strong>, which means that doesn\u2019t create its own context. Instead, it inherits the context from the place you define it.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let&#8217;s see an example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"KKyVryY\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/KKyVryY\"><br \/>\narrow function<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In the code example above the <strong>logContext<\/strong> function is an arrow function where it binds the global object to the context which is the window object.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let&#8217;s see another example.<\/span><\/p>\n<p class=\"codepen\" style=\"height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0px; padding: 1em; text-align: justify;\" data-height=\"300\" data-default-tab=\"js\" data-slug-hash=\"WNXrYdP\" data-user=\"codebitshub\">See the Pen <a href=\"https:\/\/codepen.io\/codebitshub\/pen\/WNXrYdP\"><br \/>\narrow function in nested method<\/a> by codebitshub (<a href=\"https:\/\/codepen.io\/codebitshub\">@codebitshub<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">In this example, we are using an arrow function to <strong>bind<\/strong> the context of the function <\/span><b>testThis<\/b><span style=\"font-weight: 400;\"> to <strong>myObject<\/strong> lexically. <\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">If we were using a normal function, then the context of the function <strong>testThis<\/strong> would be the window object as we already saw in the section <\/span><b><a href=\"#this_in_inner_function_of_an_object_method\">Common misunderstanding with methods<\/a>.<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">So the usage of an arrow function, in this case, is an alternative to using one of the <\/span><b>call\/apply<\/b><span style=\"font-weight: 400;\"> methods<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\"><strong>Note<\/strong>: The context of an arrow function is bound lexically and you cannot modify it even when using the apply\/call methods.<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Finally, arrow functions have some restrictions compared to normal functions.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Below is a shortlist of these restrictions as they are mentioned in the <\/span><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\"><span style=\"font-weight: 400;\">Mozilla<\/span><\/a><span style=\"font-weight: 400;\"> documentation<\/span><\/p>\n<h2 style=\"text-align: justify;\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span><span style=\"font-weight: 400;\">Summary<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Let\u2019s summarize what we discussed in this article. Below is a shortlist of the meaning of the keyword &#8216;<strong>this<\/strong>&#8216; in javascript that you should remember.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">Keep in mind that in order for you to have a deeper understanding of what &#8216;<strong>this<\/strong>&#8216; means in javascript you should think of <\/span><b>how<\/b><span style=\"font-weight: 400;\"> a function is invoked and for arrow functions what is &#8216;<strong>this<\/strong>&#8216; <\/span><span style=\"font-weight: 400;\">inside the outer function where the arrow function is declared.<\/span><\/p>\n<p style=\"text-align: justify;\"><strong>Function invocation\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\u00a0\u2192The &#8216;<strong>this<\/strong>&#8216; keyword will refer to the Global Execution Context.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><strong>Method invocation\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\u2192\u00a0 The keyword &#8216;<strong>this<\/strong>&#8216; in this case will refer to the object that owns the method.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><strong>Indirect invocation (apply\/call methods)\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\u2192 The keyword &#8216;<strong>this<\/strong>&#8216; in this case will refer to the object that is passed as the first argument in the methods call\/apply.\u00a0<\/span><\/p>\n<p style=\"text-align: justify;\"><strong>Constructor invocation\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\u2192 In this case, the &#8216;<strong>this<\/strong>&#8216; keyword will refer to the object when the object is created.<\/span><\/p>\n<p style=\"text-align: justify;\"><strong>Bound Functions\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\u2192The keyword &#8216;<strong>this<\/strong>&#8216; in this case will refer to the object that is passed in the bind method.<\/span><\/p>\n<p style=\"text-align: justify;\"><strong>Arrow functions<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-weight: 400;\">\u2192The keyword &#8216;<strong>this<\/strong>&#8216; in this case will be resolved lexically and will refer to the context of the outer function where the arrow function is declared.<\/span><\/p>\n<p>Happy coding!<\/p>\n<p style=\"text-align: justify;\"><script async src=\"https:\/\/cpwebassets.codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to comprehend the meaning of the keyword &#8216;this&#8217;\u00a0in javascript, you must first understand the meaning of the word context in the actual world. As you may know, the word context refers to the underlying concepts and facts which are required to comprehend an idea or statement.\u00a0 For example, if you take a look [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":24,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[11],"tags":[17],"post_folder":[],"class_list":["post-318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-languages","tag-javascript","post-list-post__standard"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The Complete Beginner&#039;s Guide to &#039;this&#039; in Javascript - codebitshub.com<\/title>\n<meta name=\"description\" content=\"The Complete Beginner&#039;s Guide to &#039;this&#039; in Javascript Learn how to use the &#039;this&#039; keyword in javascript through function invocation examples\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codebitshub.com\/this-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Complete Beginner&#039;s Guide to &#039;this&#039; in Javascript - codebitshub.com\" \/>\n<meta property=\"og:description\" content=\"The Complete Beginner&#039;s Guide to &#039;this&#039; in Javascript Learn how to use the &#039;this&#039; keyword in javascript through function invocation examples\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codebitshub.com\/this-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"codebitshub.com\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-29T21:46:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-04T21:22:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1125\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"myapos\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"myapos\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/\"},\"author\":{\"name\":\"myapos\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#\\\/schema\\\/person\\\/8f2b53c479aa7e7bb9a24a50b9b51e61\"},\"headline\":\"The Complete Beginner&#8217;s Guide to &#8216;this&#8217; in Javascript\",\"datePublished\":\"2022-01-29T21:46:29+00:00\",\"dateModified\":\"2022-02-04T21:22:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/\"},\"wordCount\":2104,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codebitshub.com\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/book-g6669afebe_1920.jpg\",\"keywords\":[\"javascript\"],\"articleSection\":[\"Programming Languages\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/\",\"url\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/\",\"name\":\"The Complete Beginner's Guide to 'this' in Javascript - codebitshub.com\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/codebitshub.com\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/book-g6669afebe_1920.jpg\",\"datePublished\":\"2022-01-29T21:46:29+00:00\",\"dateModified\":\"2022-02-04T21:22:09+00:00\",\"description\":\"The Complete Beginner's Guide to 'this' in Javascript Learn how to use the 'this' keyword in javascript through function invocation examples\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/codebitshub.com\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/book-g6669afebe_1920.jpg\",\"contentUrl\":\"https:\\\/\\\/codebitshub.com\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/book-g6669afebe_1920.jpg\",\"width\":1920,\"height\":1125,\"caption\":\"codebitshub.com\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/this-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codebitshub.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Complete Beginner&#8217;s Guide to &#8216;this&#8217; in Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#website\",\"url\":\"https:\\\/\\\/codebitshub.com\\\/\",\"name\":\"codebitshub.com\",\"description\":\"A blog about programming and coding\",\"publisher\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/codebitshub.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#organization\",\"name\":\"codebitshub.com\",\"url\":\"https:\\\/\\\/codebitshub.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"http:\\\/\\\/codebitshub.com\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/book-g6669afebe_1920.jpg\",\"contentUrl\":\"http:\\\/\\\/codebitshub.com\\\/wp-content\\\/uploads\\\/2022\\\/01\\\/book-g6669afebe_1920.jpg\",\"width\":1920,\"height\":1125,\"caption\":\"codebitshub.com\"},\"image\":{\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codebitshub.com\\\/#\\\/schema\\\/person\\\/8f2b53c479aa7e7bb9a24a50b9b51e61\",\"name\":\"myapos\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a35e4d21e2fa562dfe47fe5e67f5163509d4d01a363f2f4297aadf65b5bfc8ab?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a35e4d21e2fa562dfe47fe5e67f5163509d4d01a363f2f4297aadf65b5bfc8ab?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a35e4d21e2fa562dfe47fe5e67f5163509d4d01a363f2f4297aadf65b5bfc8ab?s=96&d=mm&r=g\",\"caption\":\"myapos\"},\"url\":\"https:\\\/\\\/codebitshub.com\\\/author\\\/myapos\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Complete Beginner's Guide to 'this' in Javascript - codebitshub.com","description":"The Complete Beginner's Guide to 'this' in Javascript Learn how to use the 'this' keyword in javascript through function invocation examples","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codebitshub.com\/this-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"The Complete Beginner's Guide to 'this' in Javascript - codebitshub.com","og_description":"The Complete Beginner's Guide to 'this' in Javascript Learn how to use the 'this' keyword in javascript through function invocation examples","og_url":"https:\/\/codebitshub.com\/this-in-javascript\/","og_site_name":"codebitshub.com","article_published_time":"2022-01-29T21:46:29+00:00","article_modified_time":"2022-02-04T21:22:09+00:00","og_image":[{"width":1920,"height":1125,"url":"https:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg","type":"image\/jpeg"}],"author":"myapos","twitter_card":"summary_large_image","twitter_misc":{"Written by":"myapos","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codebitshub.com\/this-in-javascript\/#article","isPartOf":{"@id":"https:\/\/codebitshub.com\/this-in-javascript\/"},"author":{"name":"myapos","@id":"https:\/\/codebitshub.com\/#\/schema\/person\/8f2b53c479aa7e7bb9a24a50b9b51e61"},"headline":"The Complete Beginner&#8217;s Guide to &#8216;this&#8217; in Javascript","datePublished":"2022-01-29T21:46:29+00:00","dateModified":"2022-02-04T21:22:09+00:00","mainEntityOfPage":{"@id":"https:\/\/codebitshub.com\/this-in-javascript\/"},"wordCount":2104,"commentCount":0,"publisher":{"@id":"https:\/\/codebitshub.com\/#organization"},"image":{"@id":"https:\/\/codebitshub.com\/this-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg","keywords":["javascript"],"articleSection":["Programming Languages"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codebitshub.com\/this-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codebitshub.com\/this-in-javascript\/","url":"https:\/\/codebitshub.com\/this-in-javascript\/","name":"The Complete Beginner's Guide to 'this' in Javascript - codebitshub.com","isPartOf":{"@id":"https:\/\/codebitshub.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codebitshub.com\/this-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/codebitshub.com\/this-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg","datePublished":"2022-01-29T21:46:29+00:00","dateModified":"2022-02-04T21:22:09+00:00","description":"The Complete Beginner's Guide to 'this' in Javascript Learn how to use the 'this' keyword in javascript through function invocation examples","breadcrumb":{"@id":"https:\/\/codebitshub.com\/this-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codebitshub.com\/this-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codebitshub.com\/this-in-javascript\/#primaryimage","url":"https:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg","contentUrl":"https:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg","width":1920,"height":1125,"caption":"codebitshub.com"},{"@type":"BreadcrumbList","@id":"https:\/\/codebitshub.com\/this-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codebitshub.com\/"},{"@type":"ListItem","position":2,"name":"The Complete Beginner&#8217;s Guide to &#8216;this&#8217; in Javascript"}]},{"@type":"WebSite","@id":"https:\/\/codebitshub.com\/#website","url":"https:\/\/codebitshub.com\/","name":"codebitshub.com","description":"A blog about programming and coding","publisher":{"@id":"https:\/\/codebitshub.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codebitshub.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codebitshub.com\/#organization","name":"codebitshub.com","url":"https:\/\/codebitshub.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codebitshub.com\/#\/schema\/logo\/image\/","url":"http:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg","contentUrl":"http:\/\/codebitshub.com\/wp-content\/uploads\/2022\/01\/book-g6669afebe_1920.jpg","width":1920,"height":1125,"caption":"codebitshub.com"},"image":{"@id":"https:\/\/codebitshub.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/codebitshub.com\/#\/schema\/person\/8f2b53c479aa7e7bb9a24a50b9b51e61","name":"myapos","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a35e4d21e2fa562dfe47fe5e67f5163509d4d01a363f2f4297aadf65b5bfc8ab?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a35e4d21e2fa562dfe47fe5e67f5163509d4d01a363f2f4297aadf65b5bfc8ab?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a35e4d21e2fa562dfe47fe5e67f5163509d4d01a363f2f4297aadf65b5bfc8ab?s=96&d=mm&r=g","caption":"myapos"},"url":"https:\/\/codebitshub.com\/author\/myapos\/"}]}},"_links":{"self":[{"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":5,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":438,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/posts\/318\/revisions\/438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/media\/24"}],"wp:attachment":[{"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/tags?post=318"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/codebitshub.com\/wp-json\/wp\/v2\/post_folder?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}