Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div>
  <h1>Patrón Singleton complejo</h1>
  <p>ConfigSEO</p>
  <p id="resultado"></p>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const elem = document.querySelector('#resultado');
const configSEO = (function(el) {
  let instancia;

  // Crear propiedades y funciones privadas, estos tambien tendran acceso
  // a el objeto instance
  function privada() {
    return `Soy una funcion privada y tengo acceso a instancia gracias a closure. ${instancia.nombre}`;
  }
  // al final configSEO sera igual a esta funcion que regresamos
  return function() {
    // si instancia ya existe, solo regresamos su referencia y todo el codigo de mas abajo deja de ejecutarse
    if (typeof instancia === 'object') {
      return instancia;
    }
    
    // Estas lineas solo se ejecutan una vez cuando la instancia del singleton aun no es creada
    instancia = {
        nombre: 'Pensemosweb',
        seo: {
          descripcion: {
            limiteCaracteres: 155,
            limitePalabras: 23
          },
          titulo: {
            limiteCaracteres: 70,
            limitePalabras: 9
          }
        }
    };

    // Agregar propiedades y metodos publicos
    instancia.metodoPublico = function() {
      el.append('Soy un metodo publico, y tengo acceso a los elementos privados gracias a closure.');
      el.append(document.createElement('br'));
      el.append(privada());
    };

    return instancia;
  };
}(elem)) //invocacion inmediata y le pasamos un elemento del DOM donde imprimiremos los mensajes

const conf1 = configSEO();
const conf2 = configSEO();
conf1.metodoPublico();
elem.append(document.createElement('br'));
elem.append('conf1 y conf2 son el mismo objeto = ');
elem.append(conf1 === conf2);
elem.append(document.createElement('br'));
conf2.metodoPublico();

              
            
!
999px

Console