{"id":23,"date":"2014-05-28T12:28:20","date_gmt":"2014-05-28T12:28:20","guid":{"rendered":"http:\/\/localhost\/wordpress\/?p=23"},"modified":"2014-06-01T22:02:14","modified_gmt":"2014-06-01T22:02:14","slug":"extender-skeleton","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=23","title":{"rendered":"javascript extender (TypeExtend)"},"content":{"rendered":"<p>I have read on many pages that this is evil like eval. Everything is evil if you can&#8217;t use that.<\/p>\n<p><strong>Unsafe extend.<\/strong> In this sample I aggree with the evli idea. This is simple and dangerous.<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nString.prototype.Contains = function(s) {\r\n  return !!~this.indexOf(s);\r\n};\r\n<\/pre>\n<p><strong>Safe version.<\/strong> Better but not the best.<br \/>\nthe skeleton<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nif (!TYPE.prototype.EXTENDER_FUNCTION) {\r\n  Object.defineProperty(TYPE.prototype, 'EXTENDER_FUNCTION', {\r\n    enumerable: false,\r\n    configurable: false,\r\n    writable: false,\r\n    value: THE_FUNCTION\r\n  });\r\n};\r\n<\/pre>\n<p>and the code<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nif (!String.prototype.Contains) {\r\n  Object.defineProperty(String.prototype, 'Contains', {\r\n    enumerable: false,\r\n    configurable: false,\r\n    writable: false,\r\n    value: function (s) {\r\n      return !!~this.indexOf(s);\r\n    }\r\n  });\r\n};\r\n<\/pre>\n<p>instead of this almost nice solution do the following. Write an universal function which can create the extender with some additional safe methods like:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nfunction TypeExtend(TYPE, EXTENDER_FUNCTION, FUNCTION,\r\n                    _enumerable, _configurable, _writable){\r\n  if (!TYPE['prototype'][EXTENDER_FUNCTION]) {\r\n    Object.defineProperty(TYPE.prototype, EXTENDER_FUNCTION, {\r\n      enumerable: _enumerable,\r\n      configurable: _configurable,\r\n      writable: _writable,\r\n      value: FUNCTION\r\n    });\r\n    return true;\r\n  };\r\n  return false;\r\n};\r\n\r\nTypeExtend(String, 'Contains', function(s) { return !!~this.indexOf(s); });\r\n<\/pre>\n<p>for object extending<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\nfunction ObjectExtend(OBJECT, EXTENDER_FUNCTION, FUNCTION){\r\n  if (!OBJECT[EXTENDER_FUNCTION]) {\r\n    OBJECT[EXTENDER_FUNCTION] = FUNCTION;\r\n    return true;\r\n  };\r\n  return false;\r\n};\r\n\r\nObjectExtend(Math, 'randomBetween', \r\n  function(min, max) { return Math.random() * (max - min) + min; }\r\n);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have read on many pages that this is evil like eval. Everything is evil if you can&#8217;t use that. Unsafe extend. In this sample I aggree with the evli idea. This is simple and dangerous. Safe version. Better but not the best. the skeleton and the code instead of this almost nice solution do [&hellip;]<\/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-23","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/23","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=23"}],"version-history":[{"count":16,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":293,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions\/293"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}