{"id":695,"date":"2024-07-30T20:08:07","date_gmt":"2024-07-30T18:08:07","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=695"},"modified":"2024-07-30T20:08:08","modified_gmt":"2024-07-30T18:08:08","slug":"004-metodos-estaticos-getters-y-setters","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/05-programacion-orientada-a-objetos\/004-metodos-estaticos-getters-y-setters\/","title":{"rendered":"004. M\u00e9todos est\u00e1ticos, getters y setters"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Si venimos de otros lenguajes de programaci\u00f3n, hay algunas caracter\u00edsticas que en <strong>Javascript<\/strong> no tenemos, como por ejm las <strong>clases privadas<\/strong>. Una <strong>clase privada<\/strong> es aquella que los <strong>atributos<\/strong> y <strong>m\u00e9todos<\/strong> que tengamos dentro de la <strong>clase<\/strong> s\u00f3lo se puedan ver dentro del cuerpo de la <strong>clase<\/strong>. Todas las clases que generamos en <strong>Javascript<\/strong> son p\u00fablicas, por ello no es necesario ponerle la palabra <strong><em>public<\/em><\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">M\u00e9todos est\u00e1ticos<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hay ciertas caracter\u00edsticas que s\u00ed existen en la escritura de <strong>clases<\/strong> en <strong>Javascript<\/strong>, una de ellas son los <strong>m\u00e9todos est\u00e1ticos<\/strong>. Los <strong>m\u00e9todos est\u00e1ticos<\/strong> son aquellos que se pueden ejecutar sin la necesidad de <strong>instanciar<\/strong> la <strong>clase<\/strong>. La sintaxis es la siguiente.<\/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;M\u00e9todos est\u00e1ticos&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;M\u00e9todos est\u00e1ticos&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; class Nombre {\n&nbsp; &nbsp; &nbsp; &nbsp; constructor(nombre, apellido) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.nombre = nombre;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.apellido = apellido;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; mostrar() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`Nombre: ${this.nombre} - Apellido: ${this.apellido}`);\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; static saludar() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Hola que tal\");\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; }\n\n  &nbsp; &nbsp; \/* Llamamos al m\u00e9todo est\u00e1tico con la siguiente sintaxis\n      El nombre de la clase, un punto y la funci\u00f3n est\u00e1tica *\/\n      Nombre.saludar();\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\">M\u00e9todos getters y setters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Los <strong>getters<\/strong> y <strong>setters<\/strong> son <strong>m\u00e9todos<\/strong> especiales que permiten establecer y obtener los valores de los <strong>atributos<\/strong> de nuestra <strong>clase<\/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=\"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;Getters y Setters en Javascript&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Getters y Setters en Javascript&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; class Animal {\n&nbsp; &nbsp; &nbsp; &nbsp; constructor(nombre, edad) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.nombre = nombre;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.edad = edad;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.raza = null;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; mostrar() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`Nombre: ${this.nombre} - Edad: ${this.edad}`);\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; get getRaza() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.raza;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; set setRaza(raza) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.raza = raza;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; const perro = new Animal(\"Perro\", 51);\n&nbsp; &nbsp; &nbsp; perro.setRaza = \"Doberman\";\n&nbsp; &nbsp; &nbsp; console.log(perro.getRaza);\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\">Aunque <em>getRaza()<\/em> es un <strong>m\u00e9todo obtenedor<\/strong>, <strong>Javascript<\/strong>, cuando llamamos al <strong>m\u00e9todo<\/strong> lo tenemos que tratar como si fuera una <strong>propiedad<\/strong>. Lo mismo ocurre con <em>setRaza()<\/em>, lo trata como una <strong>propiedad<\/strong>. El <strong>m\u00e9todo est\u00e1tico<\/strong>, al poderse utilizar sin necesidad de <strong>instanciar<\/strong>, no lo encontraremos dentro del <strong>prototipo<\/strong>. Es una caracter\u00edstica de los <strong>m\u00e9todos est\u00e1ticos<\/strong> en <strong>Javascript<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Si venimos de otros lenguajes de programaci\u00f3n, hay algunas caracter\u00edsticas que en Javascript no tenemos, como por ejm las clases privadas. Una&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":685,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-695","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":"Si venimos de otros lenguajes de programaci\u00f3n, hay algunas caracter\u00edsticas que en Javascript no tenemos, como por ejm las clases privadas. Una...","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/695","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=695"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/695\/revisions"}],"predecessor-version":[{"id":696,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/695\/revisions\/696"}],"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=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}