{"id":21868,"date":"2022-11-16T09:51:59","date_gmt":"2022-11-16T04:21:59","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/"},"modified":"2022-11-16T09:51:59","modified_gmt":"2022-11-16T04:21:59","slug":"solved-use-of-this-in-javascript-object-methods","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/","title":{"rendered":"[Solved] Use of &#8216;this&#8217; in javascript object methods"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-40150035\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"40150035\" data-parentid=\"39681767\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<blockquote>\n<p>Why can&#8217;t the context be gleaned by the j\/s?<\/p>\n<\/blockquote>\n<p>This is not a question about can and cannot. The Javascript engine is <em>designed<\/em> to use certain concepts such as for example <code>scope<\/code> and <code>execution context<\/code>. It is difficult to give a full explanation of these concept in just a few lines. But here are the (very) broad lines.<\/p>\n<h2>Scope<\/h2>\n<p>Javascript uses lexical scope. <\/p>\n<p>This is a concept defining which variables any given <code>function<\/code> has access to. This is determined by how you <em>write your code<\/em>.  It follows from this concept that any given function has access to variables placed by you:<\/p>\n<ul>\n<li>in the  global scope;<\/li>\n<li>in any function containing the function in question.<\/li>\n<\/ul>\n<p>This can be illustrated by the following example:<\/p>\n<pre><code>var one=\"one\" \/\/in the global scope\n\nfunction A () { \/\/ access to one and two\n    var two = 'two'\n\n    function B () { \/\/ access to one, two and three\n        var three=\"three\"}\n\n        function C () { \/\/ access to one, two, three and four\n            var four=\"four\"\n        };\n    };\n};\n<\/code><\/pre>\n<p>If you place a <code>function<\/code> as a <code>value<\/code> of an <code>property<\/code> on an <code>object<\/code> it becomes a <code>method<\/code>. A method only has access to the global scope (disregarding functions nested inside a method) and variables inside itself:<\/p>\n<pre><code>var one=\"one\"\n\nvar myObj = { \/\/ in the global scope\n\n     myFunctions: {\n         myFunction1: function() {}, \/\/ have access to one and myObj\n         myFunction2: function() {}  \/\/ have access to one and myObj\n         }\n     };\n<\/code><\/pre>\n<p>That is why you in your example would have to reach <code>m1<\/code> by calling <code>o.m1()<\/code>;<\/p>\n<h2>Execution context<\/h2>\n<p>A function always has a <code>this<\/code> value (disregarding arrow functions). <\/p>\n<p>The value of <code>this<\/code> is always <em>properties inside an object<\/em>. The determination of which object depends on the function&#8217;s execution context. The execution context more specifically depends on the function&#8217;s <em>callsite<\/em> as the value of <code>this<\/code> will be set to:<\/p>\n<ul>\n<li>the object (level) containing the call of the function; or if not contained within another function<\/li>\n<li>the global object<\/li>\n<\/ul>\n<p>This can be illustrated by an example:<\/p>\n<pre><code>var one=\"one\"\nvar myObj = {\n\n    two: 'two',\n    three: 'three',\n    myFunction: function () { \/\/ this points to myFunction, two and three\n                    function () { \/\/this points to one and myObj \n                    }\n                };\n<\/code><\/pre>\n<p>Now the above example is actually what can really lead you to think that now you understand <code>this<\/code> and therefore you lower your guards and before you know of it <code>this<\/code> is biting you in your a**. It is <strong><em>extremely important<\/em><\/strong> to remember the part about the function&#8217;s <strong><em>callsite<\/em><\/strong>: <\/p>\n<pre><code>var myObj = {\n    name: 'myObj',\n    myFunction: function() {console.log(this.name)\n    };\n\nvar anotherObj = {\n    name: 'anotherObj',\n    notAnotherFunction: myObj.myFunction\n    };\n\nmyObj.Myfunction(); \/\/ myObj\nanotherObj.notAnotherFunction(); \/\/ anotherObj\n<\/code><\/pre>\n<p>In the above example <em>the same function<\/em> is called twice but with different callsite&#8217;s and consequently with different values of <code>this<\/code>.<\/p>\n<hr>\n<p>I can recommend you reading <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/getify\/You-Dont-Know-JS\">You don&#8217;t know JS<\/a> series if you want to selfstudy to get to the bottom of amongst others the above concepts.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Use of &#8216;this&#8217; in javascript object methods <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Why can&#8217;t the context be gleaned by the j\/s? This is not a question about can and cannot. The Javascript engine is designed to use certain concepts such as for example scope and execution context. It is difficult to give a full explanation of these concept in just a few lines. But here are &#8230; <a title=\"[Solved] Use of &#8216;this&#8217; in javascript object methods\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\" aria-label=\"More on [Solved] Use of &#8216;this&#8217; in javascript object methods\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333,397],"class_list":["post-21868","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript","tag-this"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Use of &#039;this&#039; in javascript object methods - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Use of &#039;this&#039; in javascript object methods - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Why can&#8217;t the context be gleaned by the j\/s? This is not a question about can and cannot. The Javascript engine is designed to use certain concepts such as for example scope and execution context. It is difficult to give a full explanation of these concept in just a few lines. But here are ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-16T04:21:59+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Use of &#8216;this&#8217; in javascript object methods\",\"datePublished\":\"2022-11-16T04:21:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\"},\"wordCount\":355,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"javascript\",\"this\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\",\"name\":\"[Solved] Use of 'this' in javascript object methods - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-16T04:21:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Use of &#8216;this&#8217; in javascript object methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Use of 'this' in javascript object methods - JassWeb","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:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Use of 'this' in javascript object methods - JassWeb","og_description":"[ad_1] Why can&#8217;t the context be gleaned by the j\/s? This is not a question about can and cannot. The Javascript engine is designed to use certain concepts such as for example scope and execution context. It is difficult to give a full explanation of these concept in just a few lines. But here are ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/","og_site_name":"JassWeb","article_published_time":"2022-11-16T04:21:59+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Use of &#8216;this&#8217; in javascript object methods","datePublished":"2022-11-16T04:21:59+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/"},"wordCount":355,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript","this"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/","url":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/","name":"[Solved] Use of 'this' in javascript object methods - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-16T04:21:59+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-use-of-this-in-javascript-object-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Use of &#8216;this&#8217; in javascript object methods"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=21868"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21868\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=21868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=21868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=21868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}