{"id":692,"date":"2024-07-30T20:06:59","date_gmt":"2024-07-30T18:06:59","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=692"},"modified":"2024-07-30T20:07:00","modified_gmt":"2024-07-30T18:07:00","slug":"003-clases-y-herencia","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/05-programacion-orientada-a-objetos\/003-clases-y-herencia\/","title":{"rendered":"003. Clases y herencia"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Desde el a\u00f1o 2015, el est\u00e1ndar <strong>ES6<\/strong> nos ha dado lo que se conoce como <a href=\"https:\/\/sutilweb.eu\/2023\/07\/15\/azucar-sintactica-en-programacion-una-dulce-mejora-para-la-productividad\/\"><strong>az\u00facar sint\u00e1ctico<\/strong><\/a>, lo que significa que nosotros en <strong>Javascript<\/strong> ya podemos escribir <strong>clases<\/strong>, sin embargo esas clases, aunque nosotros <strong>gramatical<\/strong> y <strong>sint\u00e1cticamente<\/strong> escribamos dichas <strong>clases<\/strong>, <strong>Javascript<\/strong>, tras bambalinas lo va a compilar a <strong>prototipos<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">Si venimos de otros lenguajes como <strong>PHP<\/strong>, <strong>Java<\/strong>&#8230; vamos a ver que es muy similar. 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\">class Persona {\n   constructor(nombre, apellido) {\n      this.nombre = nombre;\n      this.apellido = apellido;\n   }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Las <strong>clases<\/strong> no reciben <strong>par\u00e1metros<\/strong>, para poder recibirlos a la hora que creamos un <strong>objeto<\/strong> de este tipo de <strong>clase<\/strong>, lo que hacemos es utilizar un m\u00e9todo especial denominado <strong><em>constructor<\/em><\/strong>, que es el que va a recibir dichas propiedades. Dicho <strong><em>constructor<\/em> <\/strong>se ejecuta en el momento de crear <strong>objetos<\/strong> (<strong>instancias<\/strong>) de este tipo.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Los <strong>m\u00e9todos<\/strong> se crean como vimos en cap\u00edtulos anteriores, con la siguiente&nbsp; sintaxis.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">hablar(){\n   console.log(\"Estoy hablando\");\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Los <strong>m\u00e9todos<\/strong> s\u00ed pueden recibir <strong>par\u00e1metros<\/strong> en caso de que sea necesario.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lo que hacen las <strong>clases<\/strong> es simplificar la escritura que vimos en el cap\u00edtulo anterior cuando cre\u00e1bamos las <strong>funciones protot\u00edpicas<\/strong>. En el caso de las <strong>clases<\/strong>, lo <strong>m\u00e9todos<\/strong> no es necesario sacarlos, <strong>Javascript<\/strong> realiza esta labor por nosotros tras bambalinas. Lo que tenemos es que <strong>invocar<\/strong> nuestras <strong>variables<\/strong>.<\/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=\"es\"&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;Clases y herencia en Javascript&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Clases y herencia en Javascript&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; class Persona {\n&nbsp; &nbsp; &nbsp; &nbsp; constructor(nombre, apellidos) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.nombre = nombre;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.apellidos = apellidos;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; hablar() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Soy una persona y estoy hablando\");\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; const persona = new Persona(\"Francisco\", \"Paredes\");\n&nbsp; &nbsp; &nbsp; console.log(persona);\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Herencia<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Para crear <strong>clases<\/strong> que hereden de <strong>clases<\/strong> padre se utiliza la palabra reservada <strong><em>extends,<\/em><\/strong> como en otros lenguajes de programaci\u00f3n. De la siguiente manera.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Francisco extends Persona {\n  constructor(nombre, apellidos, edad) {\n     \/* Con el m\u00e9todo super() se manda llamar al constructor\n     de la clase padre *\/\n     super(nombre, apellidos);\n     this.edad = edad;\n  }\n\n  hablar(){\n    console.log(\"Soy Francisco y estoy hablando\");\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Toda esta sintaxis no convierte a <strong>Javascript<\/strong> en un lenguaje orientado a <strong>objetos<\/strong> basado en <strong>clases<\/strong>, <strong>Javascript<\/strong> sigue siendo un lenguaje que la orientaci\u00f3n a <strong>objetos<\/strong> la basa en <strong>prototipos<\/strong>. Lo \u00fanico que nos dio el est\u00e1ndar a partir de <strong>ES6<\/strong> es poder escribir de manera m\u00e1s sencilla las <strong>clases<\/strong>, es por ello que se le llama az\u00facar sint\u00e1ctico, porque es una simplificaci\u00f3n de c\u00f3mo escribir <strong>prototipos<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Os dejamos un ejm completo.<\/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=\"es\"&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;Clases y herencia en Javascript&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Clases y herencia en Javascript&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; class Persona {\n&nbsp; &nbsp; &nbsp; &nbsp; constructor(nombre, apellidos) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.nombre = nombre;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.apellidos = apellidos;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; hablar() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Soy una persona y estoy hablando\");\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; class Francisco extends Persona {\n&nbsp; &nbsp; &nbsp; &nbsp; constructor(nombre, apellidos, edad) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(nombre, apellidos);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.edad = edad;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; hablar() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Soy Francisco y estoy hablando\");\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; const persona = new Persona(\"Nombre de persona\", \"Apellidos de persona\");\n&nbsp; &nbsp; &nbsp; console.log(persona);\n&nbsp; &nbsp; &nbsp; persona.hablar();\n\n&nbsp; &nbsp; &nbsp; const francisco = new Francisco(\"Francisco\", \"Paredes\", 51);\n&nbsp; &nbsp; &nbsp; console.log(francisco);\n&nbsp; &nbsp; &nbsp; francisco.hablar();\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Desde el a\u00f1o 2015, el est\u00e1ndar ES6 nos ha dado lo que se conoce como az\u00facar sint\u00e1ctico, lo que significa que nosotros&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":685,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-692","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":"Desde el a\u00f1o 2015, el est\u00e1ndar ES6 nos ha dado lo que se conoce como az\u00facar sint\u00e1ctico, lo que significa que nosotros...","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/692","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=692"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/692\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/692\/revisions\/694"}],"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=692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}