According to the test result, they are the same, and it is officially called an object. Hense in the rest of text, I will use
. when referring to properties to make it clear.
In the above example, the class of an object is defined in a one-off manner. To declare types that can be reusable: we use constructor and
A constructor is a special function used for creating an object:
new before a function, we turn the function into a constructor that constructs an object.
new falls into the
From the experiment above, we can observe that without
window, thus, the global variables(
birth) have been modified within the misused constructor;
- the misused
StarkIndustries()returns a undefine.
So it is dangerous to call a function designed as a constructor without
new. And a common practice is doing sanity check inside the constructor:
Or you can use real constructor inside a real class.
new is missed here:
The code triggers an error when we are about to create
class is safer than the pure constructor.
In the following example we examine more dictionary facets of an object. In particular, we will see how easily properties of an object can be added and removed like key-value entries.