Grundlegendes zu Crockfords Object.create-Shim

Ich habe über die Crockford-Beilage nachgelesen, um das Überschreiben von Prototypen zu verhindern, und habe verstanden, dass dies zeitweise nicht die Komplettlösung ist. Das verstehe ich auchES5 Shim kann eine praktikable Alternative dazu sein. Ich habe auch gelesenDieser Beitrag bietet eine robustere und sicherere Alternative.

Trotzdem würde ich gerne wissen, was seins istObject.create shim sagt und tut Kann mir bitte jemand sagen, ob meine Erklärungskommentare richtig sind?

<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>

Auf diese Weise kann der Prototyp des 'Stooge'-Objekts nicht überschrieben werden, wenn wir Dinge zu' another_stooge 'erweitern. Der Prototyp 'stooge' muss nicht mit 'constructor' zurückgesetzt werden.

Danke im Voraus,

-k

Antworten auf die Frage(5)

Ihre Antwort auf die Frage