Links (sets the constructor of) the newly created object to another object by setting the other object as its parent prototype;
Passes the newly created object from Step 1 as the this context;
Returns this if the function doesn't return an object.
Creating a user-defined object requires two steps:
Define the object type by writing a function.
Create an instance of the object with new.
To define an object type, create a function for the object type that specifies its name and properties. An object can have a property that is itself another object. See the examples below.
When the code new *Foo*(...) is executed, the following things happen:
A new object is created, inheriting from *Foo*.prototype.
The constructor function *Foo* is called with the specified arguments, and with this bound to the newly created object. new *Foo* is equivalent to new *Foo*(), i.e. if no argument list is specified, *Foo* is called without arguments.
The object (not null, false, 3.1415 or other primitive types) returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
You can always add a property to a previously defined object. For example, the statement car1.color = "black" adds a property color to car1, and assigns it a value of "black". However, this does not affect any other objects. To add the new property to all objects of the same type, you must add the property to the definition of the Car object type.
You can add a shared property to a previously defined object type by using the Function.prototype property. This defines a property that is shared by all objects created with that function, rather than by just one instance of the object type. The following code adds a color property with value "original color" to all objects of type Car, and then overwrites that value with the string "black" only in the instance object car1. For more information, see prototype.
Description
The
new
keyword does the following things:this
context;this
if the function doesn't return an object.Creating a user-defined object requires two steps:
new
.To define an object type, create a function for the object type that specifies its name and properties. An object can have a property that is itself another object. See the examples below.
When the code
new *Foo*(...)
is executed, the following things happen:*Foo*.prototype
.*Foo*
is called with the specified arguments, and withthis
bound to the newly created object.new *Foo*
is equivalent tonew *Foo*()
, i.e. if no argument list is specified,*Foo*
is called without arguments.new
expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)You can always add a property to a previously defined object. For example, the statement
car1.color = "black"
adds a propertycolor
tocar1
, and assigns it a value of "black
". However, this does not affect any other objects. To add the new property to all objects of the same type, you must add the property to the definition of theCar
object type.You can add a shared property to a previously defined object type by using the
Function.prototype
property. This defines a property that is shared by all objects created with that function, rather than by just one instance of the object type. The following code adds a color property with value"original color"
to all objects of typeCar
, and then overwrites that value with the string "black
" only in the instance objectcar1
. For more information, see prototype.