Entendendo o calcanhar Object.create de Crockford
Eu tenho lido sobre o calço de Crockford para evitar a sobrescrita de protótipos e entendo que não é a solução definitiva de todas as vezes. Eu também entendo queCalço ES5 pode ser uma alternativa viável para isso. Eu também lieste post que fornece uma alternativa mais robusta e segura.
Ainda assim, eu gostaria de saber o queObject.create
shim está "dizendo" e depois "fazendo". Alguém pode me dizer se meus comentários de explicação estão corretos?
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.
Desta forma, o protótipo do objeto 'stooge' não pode ser sobrescrito quando aumentamos o material para 'another_stooge'. Não há necessidade de redefinir o protótipo 'stooge' usando 'construtor'.
Desde já, obrigado,
-k