Zrozumienie obiektu Crockford's Shate
Czytałem o podkładce Crockforda, aby zapobiec nadpisywaniu prototypów i rozumiem, że czasami nie jest to rozwiązanie typu end-all / be-all. Ja też to rozumiemES5 Shim może być realną alternatywą dla tego. Czytam teżten post zapewnia solidniejszą i bezpieczniejszą alternatywę.
Mimo to chciałbym wiedzieć, co onObject.create
shim „mówi”, a następnie „robi”. Czy ktoś może mi powiedzieć, czy moje wyjaśnienia mają rację?
<code>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. </code>
W ten sposób prototyp obiektu „stooge” nie może zostać nadpisany, gdy rozszerzamy materiał na „another_stooge”. Nie ma potrzeby resetowania prototypu „stooge” za pomocą „konstruktora”.
Z góry dziękuję,
-k