Javascript Reference
Categories: Operators

javascript Operators new

@March 15, 2008, 1:22 a.m.
new Firefox/Netscape/NN 2 IE 3 ECMA 1  

  

The new operator creates instances of the following ECMA standard static objects:

An expression with this operator evaluates to an instance of the object. In other words, invoking this operator makes JavaScript look for a constructor function with the same name. Thus, the new operator also works with custom objects that are formed via custom constructor functions. It also works in IE for Windows for creating instances of proprietary objects, such as ActiveX and VBArray objects.

 

Syntax rules allow naming the static object, the static object with empty parentheses, and the static object with parameters in parentheses:

var myArray = new Array;
var myArray = new Array( );
var myArray = new Array("Larry", "Moe", "Curly");
 

Only the last two examples are guaranteed to work in all scriptable browser versions. With the exception of the Date object, if you omit assigning parameters during the native object creation, the newly minted instance has only the properties that are assigned to the prototype of the static object.

 
Example
 
var now = new Date( );

Powered by Linode.