javascript interactive

JavaScript Interactive — a console crash course


4.2 Constructor Functions

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.
>>>
function MyObject() {}


  
>>>
MyObject()
undefined
Object constructor functions are normal functions...
>>>
new MyObject()
{}
...called using the new keyword. This creates a new empty object instance initialized by the constructor function.
>>>
var obj = new MyObject()

  
The object instance is created before the function executes. And is returned as the default return value.
>>>
obj instanceof MyObject
true
The new object is instanceof the constructor function, in this case MyObject.
>>>
obj.constructor
function MyObject() {}
The non-enumerable constructor property provides a back-reference to the constructor function.
>>>
obj.constructor === MyObject
true
With the constructor property, it is possible to build an alternative instanceof implementation (but not recommended).
>>>

Index 1 2 3 4 5 6 7 8