{"id":9293,"date":"2012-12-20T12:49:21","date_gmt":"2012-12-20T07:19:21","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9293"},"modified":"2016-12-19T15:30:59","modified_gmt":"2016-12-19T10:00:59","slug":"this-keyword-in-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/this-keyword-in-javascript\/","title":{"rendered":"&#8220;this&#8221; keyword in JavaScript"},"content":{"rendered":"<p>There are four patterns of invocation in <strong>JavaScript<\/strong>:<br \/>\n1. The <strong>method invocation<\/strong> pattern.<br \/>\n2. The <strong>function invocation <\/strong>pattern.<br \/>\n3. The <strong>constructor invocation<\/strong> pattern.<br \/>\n4. The <strong>apply invocation<\/strong> pattern.<\/p>\n<p style=\"padding:10px\">\n<p><strong>1. The Method Invocation Pattern:<\/strong><br \/>\nWhen a function is stored as a property of an object, we call it a method. When a method is invoked, <strong>this<\/strong> is bound to that object.<br \/>\n[js]<br \/>\nvar employee = {<br \/>\n\tsalary: 25000,<br \/>\n\tincreaseSalary: function(inc) {<br \/>\n\t\tthis.salary += inc || 5000;<br \/>\n\t}<br \/>\n};<br \/>\nemployee.increaseSalary();<br \/>\ndocument.writeln(employee.salary);<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p><strong>2. The Function invocation Pattern:<\/strong><br \/>\nWhen a function is not the property of an object, <strong>this<\/strong> is bound to the <strong>global object<\/strong>.<br \/>\n[js]<br \/>\nemployee.setBonus = function () {<br \/>\n\tvar that = this;<br \/>\n\tvar countBonus = function (inc) {<br \/>\n\t\tthat.salary += inc || that.salary*0.5;<br \/>\n\t};<br \/>\n\tcountBonus();<br \/>\n};<br \/>\nemployee.setBonus();<br \/>\ndocument.writeln(employee.salary);<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p><strong>3. The Constructor Invocation Pattern:<\/strong><br \/>\nIf a function is invoked with a new prefix, then a new object will be created with a hidden link to the value of the function\u2019s prototype member, and <strong>this<\/strong> will be bound to that new object. If the function was invoked with the new prefix and the return value is not an object, then <strong>this<\/strong> (the new object) is returned instead.<br \/>\n[js]<br \/>\nvar Person = function (name) {<br \/>\n\tthis.name = name;<br \/>\n\tthis.getName = function () {<br \/>\n\t\treturn this.name;<br \/>\n\t};<br \/>\n};<br \/>\nvar newPerson = new Person(&#8220;Amit Thakkar&#8221;); \/\/ this will refer to newly created Object.<br \/>\ndocument.writeln(newPerson.getName()); \/\/ Amit Thakkar<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p><strong>4. The Apply Invocation Pattern:<\/strong><br \/>\nThe <strong>apply<\/strong> method takes two parameters. The first is the value that should be bound to <strong>this<\/strong>. The second is an array of parameters.<br \/>\n[js]<br \/>\nvar vigil = {<br \/>\n\tname:&#8221;Vigil&#8221;<br \/>\n};<br \/>\ndocument.writeln(newPerson.getName.apply(vigil)); \/\/ Vigil<br \/>\n[\/js]<br \/>\nHere <strong>this<\/strong> is referring to vigil object and there will not be any run-time error even when the number of arguments and the number of parameters do not match. If there are too many argument values, the extra argument values will be ignored. If there are too less argument values, the undefined value will be substituted for the missing values. So here second parameter will be undefined.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are four patterns of invocation in JavaScript: 1. The method invocation pattern. 2. The function invocation pattern. 3. The constructor invocation pattern. 4. The apply invocation pattern. 1. The Method Invocation Pattern: When a function is stored as a property of an object, we call it a method. When a method is invoked, this [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[1121,55,1120],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9293"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9293"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9293\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}