Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is perfectly valid Javascript.</p> <p>Any javascript function (say <code>Func</code>)can be used as a constructor and the constructor invocation also requires a <code>prototype</code> property (i.e. <code>F.prototype</code> or the prototype associated with the function) . Thus (<i>almost</i>) every function has a prototype property. The value of this property (i.e. <code>Func.prototype</code>).</p> <p>Now the value of this prototype associated with the function is an object itself that has a single <i>non enumerable</i> property called constructor. And the value of this constructor property is the function object (i.e. <code>F</code> itself).</p> <p>Lets take an example.</p> <p>Say I construct a function <code>Func</code></p> <pre> var Func = function() { //your definition }; </pre> <p>Now since this can be invoked as a constructor it has to have a <code>prototype</code> property <code>Func.prototype</code> lets call this <code>proto</code>.</p> <pre> proto = Func.prototype; </pre> <p>Now the prototype has a <i>single property (that is non enumerable</i>) called constructor. This constructor has a value that is equal to the function object itself.</p> <p>Dont believe me check it like this</p> <pre> Func.prototype.constructor === Func // =>true </pre> <p>Will always return <strong>true</strong> for any function.</p> <p><strong>Now from the code you explained :</strong> So basically these two lines</p> <pre> MyObject.prototype = Object.create(MyObject.prototype); MyObject.prototype.constructor = MyObject; </pre> <p>are modifying the value of the prototye to have a constructor property with the value of <code>MyObject</code> that is defined. But that would have happened anyways in the normal course of things. But the reason could be that maybe the <code>prototype</code> of the object has been changed earlier from the class it has been inherited from. In that case would those two lines make sense.</p> <p>Hope that helps :)</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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