{"id":489,"date":"2022-11-01T20:28:01","date_gmt":"2022-11-01T14:58:01","guid":{"rendered":"https:\/\/jassweb.com\/new22\/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-2","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/","title":{"rendered":"[Solved] Javascript: When and when not to use \u201cthis\u201d"},"content":{"rendered":"<h2> Introduction <\/h2>\n<p>[ad_1]<\/p>\n<p>This article will discuss the use of the keyword &#8220;this&#8221; in Javascript. We will look at when it is appropriate to use &#8220;this&#8221; and when it is not. We will also discuss the different ways in which &#8220;this&#8221; can be used and the implications of using it incorrectly. By the end of this article, you should have a better understanding of when and when not to use &#8220;this&#8221; in Javascript.<\/p>\n<h2> Solution<\/h2>\n<p><\/p>\n<p>When to use \u201cthis\u201d:<\/p>\n<p>1. When you need to refer to the current object in a method.<br \/>\n2. When you need to refer to the object that called a function.<br \/>\n3. When you need to refer to the object that owns a function.<br \/>\n4. When you need to refer to the global object.<\/p>\n<p>When not to use \u201cthis\u201d:<\/p>\n<p>1. When you are not inside a function.<br \/>\n2. When you are not inside a method.<br \/>\n3. When you are not referring to an object.<br \/>\n4. When you are not referring to the current object. <\/p>\n<p><\/p>\n<div class=\"entry-content\" itemprop=\"text\">\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/p>\n<p><\/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<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){\n  this.fname = fname;\n  this.lname = lname;\n  this.fullName = function(){\n    return this.fname + ' ' + this.lname;\n  }\n}\n\nvar p = new Person('foo', 'bar');\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<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){\n  var self = this;\n  self.fname = fname;\n  self.lname = lname;\n  \n  \/\/ This function is private\n  function getFullName() {\n    var name=\"\";\n    var seperator=\"\";\n    if (fname) {\n      name += self.fname;\n      seperator=\" \";\n    }\n    if (lname) {\n      name += seperator + self.lname;\n    }\n    return name;\n  }\n  this.fullName = function(){\n    return getFullName();\n  }\n}\n\nvar p = new Person('foo', 'bar');\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>\n<\/div>\n<\/div>\n<p>solved Javascript: When and when not to use \u201cthis\u201d <\/p>\n<p><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/div>\n<p>[ad_2]<\/p>\n<h1>When and When Not to Use &#8220;this&#8221; in Javascript<\/h1>\n<p>The keyword &#8220;this&#8221; is a powerful tool in Javascript, but it can be tricky to use correctly. Knowing when and when not to use &#8220;this&#8221; is essential for writing effective and efficient code.<\/p>\n<h2>What is &#8220;this&#8221;?<\/h2>\n<p>In Javascript, &#8220;this&#8221; is a keyword that refers to the object that is currently executing the code. It can be used to access properties and methods of the object, as well as to call functions on the object.<\/p>\n<h2>When to Use &#8220;this&#8221;<\/h2>\n<p>The most common use of &#8220;this&#8221; is to access properties and methods of the object that is currently executing the code. For example, if you have an object called &#8220;person&#8221; with a property called &#8220;name&#8221;, you can use &#8220;this.name&#8221; to access the name property of the person object.<\/p>\n<p>Another common use of &#8220;this&#8221; is to call functions on the object. For example, if you have an object called &#8220;person&#8221; with a method called &#8220;sayHello&#8221;, you can use &#8220;this.sayHello()&#8221; to call the sayHello method on the person object.<\/p>\n<h2>When Not to Use &#8220;this&#8221;<\/h2>\n<p>It is important to note that &#8220;this&#8221; only refers to the object that is currently executing the code. If you are trying to access a property or method of a different object, you should not use &#8220;this&#8221;. Instead, you should use the name of the object you are trying to access.<\/p>\n<p>It is also important to note that &#8220;this&#8221; does not refer to the global scope. If you are trying to access a global variable or function, you should not use &#8220;this&#8221;. Instead, you should use the name of the global variable or function.<\/p>\n<h2>Conclusion<\/h2>\n<p>Knowing when and when not to use &#8220;this&#8221; is essential for writing effective and efficient code in Javascript. When used correctly, &#8220;this&#8221; can be a powerful tool for accessing properties and methods of the object that is currently executing the code. However, it is important to remember that &#8220;this&#8221; does not refer to the global scope or to other objects, and should not be used in those cases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction [ad_1] This article will discuss the use of the keyword &#8220;this&#8221; in Javascript. We will look at when it is appropriate to use &#8220;this&#8221; and when it is not. We will also discuss the different ways in which &#8220;this&#8221; can be used and the implications of using it incorrectly. By the end of this &#8230; <a title=\"[Solved] Javascript: When and when not to use \u201cthis\u201d\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/\" aria-label=\"More on [Solved] Javascript: When and when not to use \u201cthis\u201d\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[413,333,414,397],"class_list":["post-489","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 v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Javascript: When and when not to use \u201cthis\u201d - 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-2\/\" \/>\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 \u201cthis\u201d - JassWeb\" \/>\n<meta property=\"og:description\" content=\"Introduction [ad_1] This article will discuss the use of the keyword &#8220;this&#8221; in Javascript. We will look at when it is appropriate to use &#8220;this&#8221; and when it is not. We will also discuss the different ways in which &#8220;this&#8221; can be used and the implications of using it incorrectly. By the end of this ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/\" \/>\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=\"4 minutes\" \/>\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-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Javascript: When and when not to use \u201cthis\u201d\",\"datePublished\":\"2022-11-01T14:58:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/\"},\"wordCount\":711,\"commentCount\":0,\"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-2\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/\",\"name\":\"[Solved] Javascript: When and when not to use \u201cthis\u201d - 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-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/#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 \u201cthis\u201d\"}]},{\"@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] Javascript: When and when not to use \u201cthis\u201d - 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-2\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Javascript: When and when not to use \u201cthis\u201d - JassWeb","og_description":"Introduction [ad_1] This article will discuss the use of the keyword &#8220;this&#8221; in Javascript. We will look at when it is appropriate to use &#8220;this&#8221; and when it is not. We will also discuss the different ways in which &#8220;this&#8221; can be used and the implications of using it incorrectly. By the end of this ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/","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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Javascript: When and when not to use \u201cthis\u201d","datePublished":"2022-11-01T14:58:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/"},"wordCount":711,"commentCount":0,"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-2\/","url":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/","name":"[Solved] Javascript: When and when not to use \u201cthis\u201d - 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-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-javascript-when-and-when-not-to-use-this-2\/#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 \u201cthis\u201d"}]},{"@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\/489","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=489"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}