Console Learner — An interactive JavaScript course.


4.2 Constructor Functions

>>>
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 a 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).
>>>

The console allows you to interact with the course material and examples. Use the following keys:

A special logging function is also available:


Need more magic? Check out the other developer tools on this site.