Java Reference
In-Depth Information
This can also have the unfortunate side effect of assigning all the properties
and methods to the global object if strict mode isn't used:
<< 6
One way around this is to add the following line to the beginning of a con-
structor function (in this example, the constructor function is called Con-
structor ):
if((!this instanceof Constructor)) {
return new Constructor;
This checks to see if this (the object that will eventually be returned) is
an instance of the Constructor . If it isn't, it just invokes the constructor
function again, but this time using the new operator.
Be wary that this method will rectify any mistakes where the new keyword
has not been used, but it won't help to identify where the sloppy program-
ming happened. An alternative would be to throw an error if this happens
instead—which is what happens if strict mode is used and new isn't used.
Note: Built-n Constructor Functions
JavaScript contains a number of built-in constructor functions such as Ob-
ject , Array , and Function that can be used to create objects, arrays,
and functions instead of literals.
The easiest way to create a new object is to use the literal syntax:
literal = {};
<< {}
It is possible to use the Object constructor function:
Search WWH ::

Custom Search