Para esta tarea, necesito implementar Object.create polyfill.
Object.create: crea y devuelve un nuevo objeto cuyo prototipo es el primer argumento que se pasa a la función. Si se pasa un segundo argumento, lo establece como propiedades para el nuevo objeto.
La función debe tomar dos parámetros:
prototipo (obligatorio) : un objeto, o nulo (pero no indefinido), que será el prototipo del objeto creado.
properties (opcional) : un argumento que establecerá propiedades para el nuevo objeto (se pasará a Object.defineProperties ).
Si no hay parámetros de función o el prototipo NO es un objeto o es nulo, se debe generar un TypeError .
Como resultado, Object.create devolverá el objeto creado con la propiedad interna [[Prototype]] establecida en el valor pasado en el argumento prototipo. Si se pasan propiedades y NO está indefinido, se llamará a Object.defineProperties (obj, properties) , donde obj es el objeto que se devolverá desde Object.create .
Ejemplo:
const A = { objectName: 'Object A', getObjectName: function() { return `This is ${this.objectName}!`; }, }; const B = Object.create(A, { objectName: { value: 'Object B', }, }); A.getObjectName(); // This is Object A! B.getObjectName(); // This is Object B! A.hasOwnProperty('getObjectName'); // true A.hasOwnProperty('objectName'); // true B.hasOwnProperty('getObjectName'); // false B.hasOwnProperty('objectName'); // trueLa solución a la tarea debe escribirse en esta función:
Object.create = function(proto, propertiesObject) { //code here }Mi intento:
Object.create = function(proto, propertiesObject) { if (typeof proto !== 'object' && typeof proto !== 'function') { throw new TypeError(); } else if (proto === null) { return {}; } if (typeof propertiesObject != 'undefined') return function F() {} F.prototype = proto; return new F(); };Solo fallan 2 puntos en la prueba (donde hay cruces):
✕ must return an empty object when called with a null argument (10ms) ✓ `prototype` argument works as expected (1ms) ✕ the `properties` argument works as expected (1ms) ✓ object A must be the prototype of object B (1ms) ✓ objects A and B must be different (1ms) ✓ without arguments there must be a TypeError (2ms) ✓ if the first parameter is not an object or null then there must be a TypeError (2ms)ayúdame por favor hazlo bien