Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding Crockford's Object.create shim
    primarykey
    data
    text
    <p>I've been reading up on the Crockford shim for preventing the overwriting of prototypes, and understand that it's not the end-all/be-all solution at times. I also understand that <a href="https://github.com/kriskowal/es5-shim" rel="nofollow noreferrer">ES5 Shim</a> may be a viable alternative to this. I also read <a href="https://stackoverflow.com/questions/3830800/object-defineproperty-in-es5#3844768">this post which provides a more robust, secure alternative</a>.</p> <p>Still, I'd like to know what his <code>Object.create</code> shim is "saying" and then "doing." Can someone please tell me if my explanation comments are right?</p> <pre><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></pre> <p>This way, the prototype of the 'stooge' object can't be overwritten when we augment stuff to 'another_stooge'. No need to reset the 'stooge' prototype using 'constructor'.</p> <p>Thanks in advance,</p> <p>-k</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload