{"id":1078,"date":"2012-05-30T00:56:25","date_gmt":"2012-05-30T06:56:25","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1078"},"modified":"2012-05-30T01:04:07","modified_gmt":"2012-05-30T07:04:07","slug":"code-of-the-day-javascript-create-object-without-calling-new-on-the-constructor","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/05\/code-of-the-day-javascript-create-object-without-calling-new-on-the-constructor\/","title":{"rendered":"Code of the Day: Javascript, create object without calling new on the Constructor"},"content":{"rendered":"<p>Here&#8217;s a simple trick to avoid having to call new on a constructor. All you have to do is have the constructor do it for you. Here&#8217;s how.<\/p>\n<blockquote><p>\nCheck if the `this` variable is an instanceof of the Constructor. If not, then return a new instance of the constructor whiling passing the same arguments. `new ConstructorName(arg1, arg2, &#8230; argN)`.\n<\/p><\/blockquote>\n<p>If you call a constructor without using this trick, `this` will be referenced to the scope to which the constructor is contained within. Which in most cases is `window`.<\/p>\n<p><b>Example:<\/b><br \/>\n<code><\/p>\n<pre lang='javascript'>\r\nvar Person = function( name ){\r\n    if(!(this instanceof Person)){\r\n        return new Person(name);\r\n    }\r\n    this.name = name || \"NA\" ;\r\n    return this;\r\n};\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Live Demo:<\/b>*click the results tab*<br \/>\n<iframe style=\"width: 100%; height: 300px\" src=\"http:\/\/jsfiddle.net\/8U9kG\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a simple trick to avoid having to call new on a constructor. All you have to do is have the constructor do it for you. Here&#8217;s how. Check if the `this` variable is an instanceof of the Constructor. If not, then return a new instance of the constructor whiling passing the same arguments. `new &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/05\/code-of-the-day-javascript-create-object-without-calling-new-on-the-constructor\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Javascript, create object without calling new on the Constructor<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[30,164,16],"class_list":["post-1078","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials","tag-code-of-the-day","tag-javascript","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1078"}],"version-history":[{"count":6,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1078\/revisions"}],"predecessor-version":[{"id":1084,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1078\/revisions\/1084"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}