JavaScript – Object Creation Expressions

An object creation expression creates a new
object and invokes a function (called a constructor) to initialize the
properties of that object. Object creation expressions are like
invocation expressions except that they are prefixed with the keyword

new Object()
new Point(2,3)

If no arguments are passed to the constructor function in an
object creation expression, the empty pair of parentheses can be

new Object
new Date

When an object creation expression is evaluated, JavaScript
first creates a new empty object, just like the one created by the
object initializer {}. Next, it
invokes the specified function with the specified arguments, passing
the new object as the value of the this keyword. The function can then use
this to initialize the properties
of the newly created object. Functions written for use as constructors
do not return a value, and the value of the object creation expression
is the newly created and initialized object. If a constructor does
return an object value, that value becomes the value of the object
creation expression and the newly created object is discarded.

Constructors are explained in more detail in ChapterĀ 9.

Comments are closed.