{"id":690,"date":"2024-07-30T20:05:58","date_gmt":"2024-07-30T18:05:58","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=690"},"modified":"2024-07-30T20:05:58","modified_gmt":"2024-07-30T18:05:58","slug":"002-herencia-prototipica","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/05-programacion-orientada-a-objetos\/002-herencia-prototipica\/","title":{"rendered":"002. Herencia protot\u00edpica"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">En el cap\u00edtulo anterior estuvimos viendo una introducci\u00f3n a la <strong>POO<\/strong> (<strong>programaci\u00f3n orientada a objetos<\/strong>) y a que <strong>Javascript<\/strong> es un lenguaje que utiliza el paradigma de la <strong>programaci\u00f3n orientada a objetos<\/strong> no como la mayor\u00eda de los lenguajes basados en <strong>clases<\/strong>, sino basado en <strong>prototipos<\/strong>. Tambi\u00e9n vimos como crear una <strong>funci\u00f3n constructora<\/strong> que genere un <strong>prototipo<\/strong>, y adem\u00e1s como optimizar la creaci\u00f3n de <strong>m\u00e9todos<\/strong> para estos <strong>prototipos<\/strong>, para que cuando estemos generando <strong>objetos<\/strong>, es decir, <strong>instancias<\/strong> de nuestra <strong>funci\u00f3n protot\u00edpica<\/strong>, ahorremos memoria de nuestra aplicaci\u00f3n al no duplicar en espacio de memoria los <strong>m\u00e9todos<\/strong> que se pueden asignar directamente a los <strong>prototipos<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Herencia<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">La <strong>herencia<\/strong> es la capacidad de poder heredar caracter\u00edsticas de un padre a un hijo, esto se da con los <strong>lenguajes basados en clases<\/strong>, pero en <strong>Javascript<\/strong> que est\u00e1 basado en <strong>prototipos<\/strong>, la <strong>herencia<\/strong> se da a modo de cadena <strong>protot\u00edpica<\/strong>. Veamos un ejm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&nbsp; &lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\" \/&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n&nbsp; &nbsp; &lt;title&gt;Herencia protot\u00edpica&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Herencia protot\u00edpica&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; function Persona(nombre, edad) {\n&nbsp; &nbsp; &nbsp; &nbsp; this.nombre = nombre;\n&nbsp; &nbsp; &nbsp; &nbsp; this.edad = edad;\n&nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; Persona.prototype.hablar = function () {\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Soy una persona y estoy hablando\");\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; \/\/ Herencia protot\u00edpica\n&nbsp; &nbsp; &nbsp; function Hombre(nombre, edad, apellidos) {\n&nbsp; &nbsp; &nbsp; &nbsp; this.super = Persona;\n&nbsp; &nbsp; &nbsp; &nbsp; this.super(nombre, edad);\n&nbsp; &nbsp; &nbsp; &nbsp; this.apellidos = apellidos;\n&nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; \/\/ Hombre est\u00e1 heredando de Persona\n      \/\/ Muy importantes estas dos lineas de c\u00f3digo siguientes\n&nbsp; &nbsp; &nbsp; Hombre.prototype = new Persona();\n&nbsp; &nbsp; &nbsp; Hombre.prototype.constructor = Hombre;\n\n&nbsp; &nbsp; &nbsp; \/\/ Sobreescritura de m\u00e9todo del prototipo padre en el hijo\n&nbsp; &nbsp; &nbsp; Hombre.prototype.hablar = function () {\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Soy un hombre y estoy hablando\");\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; \/\/ Nueva funci\u00f3n no inclu\u00edda en la funci\u00f3n protot\u00edpica padre\n      Hombre.prototype.saludar = function () {\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Soy un hombre y estoy saludando\");\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; const francisco = new Hombre(\"Francisco\", 51, \"Paredes\");\n&nbsp; &nbsp; &nbsp; console.log(francisco);\n      francisco.hablar();\n      francisco.saludar();\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Desde el a\u00f1o 2015 vamos a poder hacer lo mismo en <strong>Javascript<\/strong> que en otros lenguajes de programaci\u00f3n con las <strong>clases<\/strong>. <strong>Javascript<\/strong>, durante toda su existencia y en futuro pr\u00f3ximo, seguir\u00e1 trabajando la parte de la <strong>POO<\/strong> basada en <strong>prototipos<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Si resulta confuso este tema de como crear <strong>prototipos<\/strong> y la <strong>herencia protot\u00edpica<\/strong>, no hay que preocuparse ya que en los pr\u00f3ximos cap\u00edtulos trabajaremos con <strong>clases<\/strong>, las cuales, tras bambalinas realizar\u00e1n el cambio a <strong>prototipos<\/strong> sin que nosotros tengamos que crear las <strong>funciones protot\u00edpicas<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En el cap\u00edtulo anterior estuvimos viendo una introducci\u00f3n a la POO (programaci\u00f3n orientada a objetos) y a que Javascript es un lenguaje&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":685,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-690","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En el cap\u00edtulo anterior estuvimos viendo una introducci\u00f3n a la POO (programaci\u00f3n orientada a objetos) y a que Javascript es un lenguaje...","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=690"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/690\/revisions"}],"predecessor-version":[{"id":691,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/690\/revisions\/691"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/685"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}