Alas, with subclassing, you have no choice and have to set the constructor property yourself. > Object.getOwnPropertyNames(f.prototype)īest practice: Avoid replacing the complete prototype value of a constructor with your own object and only add new properties to it. JavaScript even sets up that property for you: > Object.getOwnPropertyNames(Object.getPrototypeOf(f)) It turns out that an instance does not own the constructor property, but inherits it from its prototype: Where does the constructor property come from? Is determined by whether C.prototype is in the prototype chain of obj. Sets the prototype of this object to the constructor functions prototype property. The new keyword has to be placed before the constructor function call and will do the following things: Creates a new object. Note that the instanceof operator does not use the constructor property. Summary: The new keyword is used in JavaScript to create a object from a constructor function. It can be used to similar effect in methods: Sub.prototype = Object.create(Sub.superclass) Īssigning the super-prototype to Sub.superclass avoids the hardcoded use of the superclass name in the sub-constructor.
0 Comments
Leave a Reply. |