Entendiendo el objeto de Crockford.
He estado leyendo sobre el calzo de Crockford para prevenir la sobrescritura de prototipos, y entiendo que a veces no es la solución definitiva. Tambien entiendo queES5 Shim Puede ser una alternativa viable a esto. Yo tambien leoEste post que proporciona una alternativa más robusta y segura..
Aún así, me gustaría saber cuál es suObject.create
shim es "decir" y luego "hacer". ¿Puede alguien decirme si los comentarios de mi explicación son correctos?
if (typeof Object.create === 'undefined') {
//If the browser doesn't support Object.create
Object.create = function (o) {
//Object.create equals an anonymous function that accepts one parameter, 'o'.
function F() {};
//Create a new function called 'F' which is just an empty object.
F.prototype = o;
//the prototype of the 'F' function should point to the
//parameter of the anonymous function.
return new F();
//create a new constructor function based off of the 'F' function.
};
}
//Then, based off of the 'Lost' example in the Crockford book...
var another_stooge = Object.create(stooge);
//'another_stooge' prototypes off of 'stooge' using new school Object.create.
//But if the browser doesn't support Object.create,
//'another_stooge' prototypes off of 'stooge' using the old school method.
De esta manera, el prototipo del objeto 'stooge' no se puede sobrescribir cuando aumentamos el contenido a 'another_stooge'. No es necesario restablecer el prototipo 'stooge' usando 'constructor'.
Gracias por adelantado,
-k