<div>
<h1>Factory de singleton</h1>
<p>Jaime, Pepito y ConfigSEO</p>
<p id="resultado"></p>
</div>
//factory de singletons
const fabricaDeSingletons = (function() {
const singletons = {};
function crearSingleton(nombre, definicionFn) {
if (typeof singletons[nombre] === 'object') {
return singletons[nombre];
}
singletons[nombre] = definicionFn();
return singletons[nombre];
}
return crearSingleton;
}());
// definir tu singleton
fabricaDeSingletons('jaime', function () {
return {
nombre: 'Jaime',
edad: 29
}
});
fabricaDeSingletons('pepito', function () {
return {
nombre: 'Pepito',
edad: 5
}
});
fabricaDeSingletons('configSEO', function () {
return {
elem: document.querySelector('#resultado'),
nombre: 'Pensemosweb',
seo: {
descripcion: {
limiteCaracteres: 155,
limitePalabras: 23
},
titulo: {
limiteCaracteres: 70,
limitePalabras: 9
}
}
};
});
const elem = document.querySelector('#resultado')
// usar tu singleton
let jaime = fabricaDeSingletons('jaime')
elem.append(JSON.stringify(jaime)) // jaime
elem.append(document.createElement('br'));
let pepito = fabricaDeSingletons('pepito');
elem.append(JSON.stringify(pepito)) // pepito
elem.append(document.createElement('br'));
let configSEO = fabricaDeSingletons('configSEO');
elem.append(JSON.stringify(configSEO)) // configSEO
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.