{"id":676,"date":"2024-07-30T19:58:08","date_gmt":"2024-07-30T17:58:08","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=676"},"modified":"2024-07-30T19:58:09","modified_gmt":"2024-07-30T17:58:09","slug":"007-objetos-literales","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/04-estructuras-de-control-en-javascript\/007-objetos-literales\/","title":{"rendered":"007. Objetos literales"},"content":{"rendered":"\n<p>Los <strong>objetos literales<\/strong> son una nueva forma de escribir <strong>atributos<\/strong> y <strong>m\u00e9todos <\/strong>en <strong>objetos<\/strong>, incluso de asignarlos.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><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;Literales Javascript&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Literales Javascript&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; let nombre = \"Francisco\",\n&nbsp; &nbsp; &nbsp; &nbsp; edad = 51;\n\n&nbsp; &nbsp; &nbsp; \/\/ Antes de los objetos literales\n&nbsp; &nbsp; &nbsp; const persona = {\n&nbsp; &nbsp; &nbsp; &nbsp; nombre: nombre,\n&nbsp; &nbsp; &nbsp; &nbsp; edad: edad,\n&nbsp; &nbsp; &nbsp; &nbsp; hablar: function () {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Hola soy Francisco\");\n&nbsp; &nbsp; &nbsp; &nbsp; },\n&nbsp; &nbsp; &nbsp; };\n&nbsp; &nbsp; &nbsp; console.log(persona);\n&nbsp; &nbsp; &nbsp; persona.hablar();\n\n&nbsp; &nbsp; &nbsp; \/\/ Despu\u00e9s de los objetos literales\n&nbsp; &nbsp; &nbsp; const fran = {\n&nbsp; &nbsp; &nbsp; &nbsp; nombre,\n&nbsp; &nbsp; &nbsp; &nbsp; edad,\n&nbsp; &nbsp; &nbsp; &nbsp; hablar: function () {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Mi nombre es Fran\");\n&nbsp; &nbsp; &nbsp; &nbsp; },\n&nbsp; &nbsp; &nbsp; };\n&nbsp; &nbsp; &nbsp; console.log(fran);\n&nbsp; &nbsp; &nbsp; fran.hablar();\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<p>Con los <strong>literales<\/strong> agilizamos el proceso de escritura de nuestro <strong>objeto<\/strong>. Si queremos agregar otra <strong>propiedad<\/strong> que no tenemos guardada en una <strong>variable<\/strong>, lo \u00fanico que hacemos es incluirla en nuestro objeto de la manera habitual.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apellido: \"Paredes\",<\/pre>\n\n\n\n<p>El ejm completo quedar\u00eda como sigue.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; \/\/ Despu\u00e9s de los objetos literales\n&nbsp; &nbsp; &nbsp; const fran = {\n&nbsp; &nbsp; &nbsp; &nbsp; nombre,\n&nbsp; &nbsp; &nbsp; &nbsp; apellido: \"Paredes\",\n&nbsp; &nbsp; &nbsp; &nbsp; edad,\n&nbsp; &nbsp; &nbsp; &nbsp; hablar: function () {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Mi nombre es Fran\");\n&nbsp; &nbsp; &nbsp; &nbsp; },\n&nbsp; &nbsp; &nbsp; };\n&nbsp; &nbsp; &nbsp; console.log(fran);\n&nbsp; &nbsp; &nbsp; fran.hablar();<\/pre>\n\n\n\n<p>Esto para las <strong>propiedades<\/strong>, para los <strong>m\u00e9todos<\/strong>, la nueva forma de escribir es la siguiente.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; hablar() {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(\"Mi nombre es Fran\");\n&nbsp; &nbsp; &nbsp; &nbsp; },<\/pre>\n\n\n\n<p>Lo que hacemos es quitar los dos puntos y la palabra reservada <strong><em>function<\/em><\/strong>, utilizando la sintaxis como se indica arriba.<\/p>\n\n\n\n<p>Estos son las dos nuevas formas de escribir <strong>atributos<\/strong> y <strong>m\u00e9todos<\/strong> dentro de los objetos a partir del est\u00e1ndar <strong>ES6<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Los objetos literales son una nueva forma de escribir atributos y m\u00e9todos en objetos, incluso de asignarlos.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":658,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-676","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":"Los objetos literales son una nueva forma de escribir atributos y m\u00e9todos en objetos, incluso de asignarlos.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/676","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=676"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/676\/revisions"}],"predecessor-version":[{"id":678,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/676\/revisions\/678"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/658"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}