{"id":809,"date":"2015-07-26T20:49:19","date_gmt":"2015-07-26T20:49:19","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=809"},"modified":"2015-07-26T20:49:19","modified_gmt":"2015-07-26T20:49:19","slug":"class-with-optional-new-keyword","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=809","title":{"rendered":"Class with optional new keyword"},"content":{"rendered":"<blockquote><p>Goal: create class allow creation without the <strong><em>new<\/em><\/strong> keyword<\/p><\/blockquote>\n<p>The pattern is simple:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nmyClass = function fn( params ) { \r\n \/\/ here is the trick\r\n \/\/ without the 'new' keyword the 'this' will be not instance of my class\r\n  if ( !(this instanceof fn) ) { return new fn( params ); }\r\n \/\/ put here everything what you want\r\n \/\/ ...\r\n  return this;\r\n};\r\n<\/pre>\n<p>The creation itself is the following:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nvar a = new myClass('a');\r\n\/\/ or simple\r\nvar a = myClass('a');\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal: create class allow creation without the new keyword The pattern is simple: The creation itself is the following:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-809","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=809"}],"version-history":[{"count":1,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/809\/revisions"}],"predecessor-version":[{"id":810,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/809\/revisions\/810"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}