This Appendix lists the built-in constructor functions outlined in the ECMAScript standard, together with the properties and methods of the objects created by these constructors.
Object()
is a constructor that creates objects, for example:
>>> var o = new Object();
This is the same as using the object literal:
>>> var o = {}; // recommended
You can pass anything to the constructor and it will try to guess what it is and use a more appropriate constructor. For example, passing a string to new
Object()
will be the same as using the new
String()
constructor. This is not a recommended practise, but still possible.
>>> var o = new Object('something'), >>> o.constructor
String()
>>> var o = new Object(123); >>> o.constructor
Number()
All other objects, built-in or custom, inherit from Object. So the properties and methods discussed below apply to all types of objects.
The Array
constructor creates array objects:
>>> var a = new Array(1,2,3);
This is the same as the array literal:
>>> var a = [1,2,3]; //recommended
When you pass only one numeric value to the Array
constructor, it's assumed to be the array length. An array with this length will be created, and filled with undefined
elements.
>>> var a = new Array(3); >>> a.length
3
>>> a
[undefined, undefined, undefined]
This can sometimes lead to some unexpected behavior. For example, the following use of the array literal is valid:
>>> var a = [3.14] >>> a
[3.14]
However, passing the floating-point number to the Array
constructor is an error:
>>> var a = new Array(3.14)
invalid array length
JavaScript functions are objects. They can be defined using the Function
constructor, like so:
>>> var sum = new Function('a', 'b', 'return a + b;'),
This is equivalent to the function literal:
>>> var sum = function(a, b){return a + b;};
or the more common:
>>> function sum(a, b){return a + b;}
The use of the Function
constructor is discouraged in favor of the function literals.
The Boolean
constructor creates boolean objects (not to be confused with boolean primitives). The boolean objects are not very useful and are listed here for the sake of completeness.
>>> var b = new Boolean(); >>> b.valueOf()
false
>>> b.toString()
"false"
A boolean object is not the same as a boolean primitive value. As you know, all objects are truthy:
>>> b === false
false
>>> typeof b
"object"
Boolean objects don't have any properties other than the ones inherited from Object
.
Creates number objects:
>>> var n = new Number(101); >>> typeof n
"object"
>>> n.valueOf();
101
Number
objects are not primitive objects, but if you use a number method on a primitive number, the primitive will be converted to a Number
object behind the scenes and the code will work.
>>> var n = 123; >>> typeof n;
"number"
>>> n.toString()
"123"
The String()
constructor creates string objects. Primitive strings are turned into objects behind the scenes if you call a method on them as if they were objects.
Creating a string object and a string primitive:
>>> var s_obj = new String('something'), >>> var s_prim = 'something'; >>> typeof s_obj
"object"
>>> typeof s_prim
"string"
The object and the primitive are not equal when compared by type with ===
:
>>> s_obj === s_prim
false
>>> s_obj == s_prim
true
length
is a property of string objects:
>>> s_obj.length
9
If you access length
on a non-object but a primitive string, the primitive is converted to an object behind the scenes and the operation is successful:
>>> "something".length
9
The Date
constructor can be used with several types of input:
You can pass values for year, month, date of the month, hour, minute, second and millisecond, like so:
>>> new Date(2011, 0, 1, 13, 30, 35, 500)
Sat Jan 01 2011 13:30:35 GMT-0800 (Pacific Standard Time)
You can skip any of the input parameters, in which case they are assumed to be 0. Note that month values are from 0 (January) to 11 (December), hours are from 0 to 23, minutes and seconds 0 to 59, and milliseconds 0 to 999.
You can pass a timestamp:
>>> new Date(1293917435500)
Sat Jan 01 2011 13:30:35 GMT-0800 (Pacific Standard Time)
If you don't pass anything, the current date/time is assumed:
>>> new Date()
Fri Apr 18 2008 01:13:00 GMT-0700 (Pacific Daylight Time)
If you pass a string, it's parsed in attempt to extract a possible date value:
>>> new Date('May 4, 2008')
Sun May 04 2008 00:00:00 GMT-0700 (Pacific Daylight Time)
Math
is a little different from the other built-in objects, because it cannot be used as a constructor to create objects. It is just a collection of functions and constants. Some examples, to illustrate the difference, are given below:
>>> typeof String.prototype
"object"
>>> typeof Date.prototype
"object"
>>> typeof Math.prototype
"undefined"
>>> typeof Math
"object"
>>> typeof String
"function"
You can create a regular expression object by using the RegExp()
constructor and passing the expression pattern as the first parameter and the pattern modifiers as the second.
>>> var re = new RegExp('[dn]o+dle', 'gmi'),
This matches "noodle", "doodle", "doooodle", and so on. It's equivalent to using the regular expression literal:
>>> var re = ('/[dn]o+dle/gmi'), // recommended
Chapter 4 and Appendix D contain more information on regular expressions and patterns.
Error objects are created either by the environment (the browser) or by your code.
>>> var e = new Error('jaavcsritp is _not_ how you spell it'), >>> typeof e
"object"
Other than the Error
constructor, six additional ones exist and they all inherit Error
:
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
18.191.150.109