{"id":1024,"date":"2010-05-26T10:35:12","date_gmt":"2010-05-26T10:35:12","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1024"},"modified":"2010-05-26T10:35:12","modified_gmt":"2010-05-26T10:35:12","slug":"prototypes","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/26\/prototypes\/","title":{"rendered":"Prototypes"},"content":{"rendered":"<p>Prototypes are a great thing with javascript, and they really come into there own with <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ajax_(programming)\">ajax<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/JQuery\">jQuery<\/a>.  A prototype basically it extends the present functional aspects.  So lets say that you have a basic shape of animal (legs, head etc) and then you want to extend that basic shape with the option if the animal can fly then you can write a prototype to extend that, the prototype can either be a function or a variable so for example<\/p>\n<pre lang=\"javascript\">\r\n\/\/ variable\r\nanimal.prototype.flyable = false;\r\n\/\/ function\r\nanimal.prototype.canFly = function() {\r\n     return this.flyable;\r\n}\r\n<\/pre>\n<p>the &#8220;this.&#8221; means access the variables\/functions within the main type (in this case it would be the animal as the main type).<\/p>\n<p>in the example I am just altering some text within a HTML input tag, to start with I am creating a function (that I will extend later) and create a local (this.) variable that points to the element passed by using the document object, afterwards set the value equal to &#8220;hi from the parent&#8221;.  I called it the parent because it is the main type, <\/p>\n<pre lang=\"javascript\">\r\n  function nameing(elem)\r\n  {\r\n      this.theelem = document.getElementById(elem);\r\n      this.theelem.value = \"hi from the parent\";\r\n  }\r\n<\/pre>\n<p>then to extend this type, you start with the typename.prototype.extension_name, so the examples below will either return the value from the document element value that was setup in the &#8220;parent&#8221; \/ main type, and also the setText function will set the text to the document element.<\/p>\n<pre lang=\"javascript\">  \r\n  nameing.prototype.getText = function() \r\n  {\r\n    return this.theelem.value;\r\n  }<\/pre>\n<p>this is how I set up the object by calling new on the main type with passing in the element that I want to link to the object.<\/p>\n<pre lang=\"javascript\">\r\n  \/\/ sets up the link with the 'thewords' id element within the html page\r\n    var name = new nameing('thewords');\r\n<\/pre>\n<p>it is all very similar to other object languages, where you reference the object itself by using the &#8220;this.&#8221; syntax.  <\/p>\n<p>Below is the full html code that you can copy-paste into your favourite text editor and then open up in a web browser to see what you think.<\/p>\n<pre lang=\"html4strict\">\r\n<html>\r\n<head>\r\n<script language=\"javascript\">\r\n\/\/ a prototype \r\n  function nameing(elem)\r\n  {\r\n      this.theelem = document.getElementById(elem);\r\n      this.theelem.value = \"hi from the parent\";\r\n  }\r\n  \r\n  nameing.prototype.getText = function() \r\n  {\r\n    return this.theelem.value;\r\n  }\r\n    \r\n  nameing.prototype.setText = function(newText)\r\n  {\r\n    this.theelem.value = newText;\r\n  }\r\n  \r\n\r\n  function start()\r\n  {\r\n  \/\/ sets up the link with the 'thewords' id element within the html page\r\n    var name = new nameing('thewords');\r\n    \/\/ since the call above will set the 'thewords' to equal \"hi from the parent\"\r\n    alert(name.getText());\r\n    \/\/ now we are setting the text\r\n    name.setText(\"genux was here\");\r\n    \/\/ and again outputting the text again\r\n    alert(name.getText());\r\n  }\r\n  \r\n\r\n<\/script>\r\n<\/head>\r\n<body>\r\n  <input id=\"thewords\"\/>\r\n  <a onclick=\"javascript:start()\">Click me<\/a>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Prototypes are a great thing with javascript, and they really come into there own with ajax and jQuery. A prototype basically it extends the present functional aspects. So lets say that you have a basic shape of animal (legs, head etc) and then you want to extend that basic shape with the option if the &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/26\/prototypes\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Prototypes<\/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":[16],"tags":[214],"class_list":["post-1024","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-prototype"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1024"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1024\/revisions"}],"predecessor-version":[{"id":1027,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1024\/revisions\/1027"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}