{"id":18743,"date":"2022-11-01T20:28:01","date_gmt":"2022-11-01T14:58:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/"},"modified":"2022-11-01T20:28:01","modified_gmt":"2022-11-01T14:58:01","slug":"solved-javascript-when-and-when-not-to-use-this","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/","title":{"rendered":"[Solved] Javascript: When and when not to use &#8220;this&#8221;"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48781212\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48781212\" data-parentid=\"48780927\" data-score=\"5\" 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>when it is needed\/best practice to use the keyword this<\/p>\n<\/blockquote>\n<p>This is usually used when you want to access something in some. So for instance, if you have a custom object and want to use some property inside some method, you should use <code>this<\/code>.<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"true\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code snippet-currently-hidden\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>function Person(fname, lname){\r\n  this.fname = fname;\r\n  this.lname = lname;\r\n  this.fullName = function(){\r\n    return this.fname + ' ' + this.lname;\r\n  }\r\n}\r\n\r\nvar p = new Person('foo', 'bar');\r\nconsole.log(p.fullName())<\/code><\/pre>\n<\/div>\n<\/div>\n<p>If you see, in current constructor, I created a function(<code>fullName<\/code>) which needs to access <code>fname<\/code> and <code>lname<\/code> properties of the object it is part of. This is a place <code>this<\/code> must be used.<\/p>\n<hr>\n<blockquote>\n<p>Now while declaration, when to use <code>this<\/code>?<\/p>\n<\/blockquote>\n<p>Any property in a constructor that is a part of <code>this<\/code> will be a part of the object. So if you need something that is only accessible to you but not outside, you can use <code>function<\/code> instead of <code>this<\/code>.<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"true\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code snippet-currently-hidden\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>function Person(fname, lname){\r\n  var self = this;\r\n  self.fname = fname;\r\n  self.lname = lname;\r\n  \r\n  \/\/ This function is private\r\n  function getFullName() {\r\n    var name=\"\";\r\n    var seperator=\"\";\r\n    if (fname) {\r\n      name += self.fname;\r\n      seperator=\" \";\r\n    }\r\n    if (lname) {\r\n      name += seperator + self.lname;\r\n    }\r\n    return name;\r\n  }\r\n  this.fullName = function(){\r\n    return getFullName();\r\n  }\r\n}\r\n\r\nvar p = new Person('foo', 'bar');\r\nconsole.log(p.fullName())<\/code><\/pre>\n<\/div>\n<\/div>\n<hr>\n<p>As for your code, I have already explained in comment why it works:<\/p>\n<blockquote>\n<p>In non-strict mode, <code>this<\/code> will point to <code>window<\/code> and any declaration not in a function will be a part of global scope.<\/p>\n<\/blockquote>\n<p>But as rightly pointer by @Jonas W its a bad practice. Why it is bad?<\/p>\n<ul>\n<li>Any variable defined without declaration statement(<code>var|let|const<\/code>) will become part of global scope.<\/li>\n<li>Any variable with declaration statement outside any functions will become part of global scope.<\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Javascript: When and when not to use &#8220;this&#8221; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] when it is needed\/best practice to use the keyword this This is usually used when you want to access something in some. So for instance, if you have a custom object and want to use some property inside some method, you should use this. function Person(fname, lname){ this.fname = fname; this.lname = lname; this.fullName &#8230; <a title=\"[Solved] Javascript: When and when not to use &#8220;this&#8221;\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/\" aria-label=\"More on [Solved] Javascript: When and when not to use &#8220;this&#8221;\">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":[413,333,414,397],"class_list":["post-18743","post","type-post","status-publish","format-standard","hentry","category-solved","tag-function","tag-javascript","tag-scope","tag-this"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Javascript: When and when not to use &quot;this&quot; - 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-javascript-when-and-when-not-to-use-this\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Javascript: When and when not to use &quot;this&quot; - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] when it is needed\/best practice to use the keyword this This is usually used when you want to access something in some. So for instance, if you have a custom object and want to use some property inside some method, you should use this. function Person(fname, lname){ this.fname = fname; this.lname = lname; this.fullName ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T14:58:01+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Javascript: When and when not to use &#8220;this&#8221;\",\"datePublished\":\"2022-11-01T14:58:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/\"},\"wordCount\":210,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"function\",\"javascript\",\"scope\",\"this\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/\",\"name\":\"[Solved] Javascript: When and when not to use \\\"this\\\" - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-11-01T14:58:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-javascript-when-and-when-not-to-use-this\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Javascript: When and when not to use &#8220;this&#8221;\"}]},{\"@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\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Javascript: When and when not to use \"this\" - 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-javascript-when-and-when-not-to-use-this\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Javascript: When and when not to use \"this\" - JassWeb","og_description":"[ad_1] when it is needed\/best practice to use the keyword this This is usually used when you want to access something in some. So for instance, if you have a custom object and want to use some property inside some method, you should use this. function Person(fname, lname){ this.fname = fname; this.lname = lname; this.fullName ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T14:58:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Javascript: When and when not to use &#8220;this&#8221;","datePublished":"2022-11-01T14:58:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/"},"wordCount":210,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["function","javascript","scope","this"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/","url":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/","name":"[Solved] Javascript: When and when not to use \"this\" - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T14:58:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Javascript: When and when not to use &#8220;this&#8221;"}]},{"@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\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","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\/18743","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=18743"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18743\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}