Ho letto gli spessori di Crockford per impedire la sovrascrittura dei prototipi e capisco che a volte non è la soluzione definitiva. Capisco anche che ES5 Shim potrebbe essere una valida alternativa a questo. Leggo anche this post which provides a more robust, secure alternative.Capire lo shim Object.create di Crockford
Ancora, mi piacerebbe sapere qual è il suo spessorato Object.create
"dicendo" e quindi "facendo". Qualcuno può dirmi se i miei commenti di spiegazione sono giusti?
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.
In questo modo, il prototipo dell'oggetto 'fantoccio' non possono essere sovrascritti quando accresciamo roba da 'another_stooge'. Non c'è bisogno di resettare il prototipo 'stooge' usando 'costruttore'.
Grazie in anticipo,
-k
A-ha! Penso di aver capito. Grazie Sheikh ... il tuo aiuto è molto apprezzato !!! – kaidez
Siete i benvenuti :-) –