{"id":393,"date":"2011-07-19T12:16:30","date_gmt":"2011-07-19T12:16:30","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=393"},"modified":"2012-12-25T22:39:32","modified_gmt":"2012-12-25T22:39:32","slug":"javascript-prototype","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/javascript-prototype\/","title":{"rendered":"javascript prototype"},"content":{"rendered":"<p>I would to extend the functionality of a JavaScript class, for example, to add utility functions to a specific Object.<\/p>\n<h2>Object.prototype<\/h2>\n<p>The prototype property can be used to add new properties and functions to any object, including built-in objects. For example, you could extend an Array as follows:<\/p>\n<pre class=\"sh_javascript\">\r\n\/\/\r\n\/\/ extend Array objects, test if value is in array\r\n\/\/ e.g.,  [0,1,2].contains(2) == true\r\n\/\/        [0,1,2].contains('spam') == false\r\nArray.prototype.contains = function(obj) {\r\n  for (var i = 0; i < this.length; i++) {\r\n    if (this[i] == obj) {\r\n      return true;\r\n    }\r\n  }\r\n  return false;\r\n}\r\n<\/pre>\n<p>This will add the <em>contains()<\/em> function to all Array objects, even those already instantiated. The prototype property can be used to dynamically add new properties to all objects of a specific type, for example:<\/p>\n<pre class=\"sh_javascript\">\r\nfunction Spam(name, eggs) {\r\n  this.name = name;\r\n  this.eggs = eggs;\r\n}\r\nfunction Foo(eggs) {\r\n  this.eggs = eggs;\r\n}\r\n\r\nmyspam = new Spam('Brian', 12);\r\nmyfoo = new Foo(36);\r\n\r\nfunction eggsByDozen() {\r\n  return this.eggs \/ 12;\r\n}\r\nSpam.prototype.dozens = eggsByDozen;\r\nFoo.prototype.dozens = eggsByDozen;\r\n\r\nmyspam.dozens() \/\/ returns 1\r\nmyfoo.dozens() \/\/ returns 3\r\n<\/pre>\n<p>In this case, an existing function <em>eggsByDozen()<\/em> is applied to multiple classes, new objects and even previously instantiated objects of those types will have the new method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would to extend the functionality of a JavaScript class, for example, to add utility functions to a specific Object. Object.prototype The prototype property can be used to add new properties and functions to any object, including built-in objects. For example, you could extend an Array as follows: \/\/ \/\/ extend Array objects, test if [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/393"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=393"}],"version-history":[{"count":10,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/393\/revisions"}],"predecessor-version":[{"id":717,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/393\/revisions\/717"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}