There are certain built-in objects available whenever an ECMA Script program begins execution. One, the global object, is in the scope chain of the executing program. Others are accessible as initial properties of the global object. If that built-in object has a [[Call]] property, then by default, the [[Class]] property of a built-in object is "Function"; otherwise , the [[Class]] property specifies an "Object". Many built-in objects are functions: they can be invoked with arguments. Some of them are constructor functions intended for use with the new operator. Each built-in function is associated with required arguments and a reference to a Function object that is associated with predefined properties. Each built-in constructor has predefined properties for the prototype object of that constructor and predefined properties returned by a new expression that invokes that constructor. For additional details, the reader is encouraged to review the detailed list of native objects provided by the ECMA Script 3.0 specification. 6.9.1 Object ObjectsWhen 'Object' constructor is called as part of a new expression, it is a constructor that may create an object. The value of the internal [[Prototype]] property of the Object constructor is the Function prototype object. In addition to the internal properties and the length property (whose value is 1), the Object constructor has the following properties:
6.9.2 Joined ObjectsJoined objects provide an optimization method for improving performance and reducing memory consumption. Two or more objects joined to each other are effectively indistinguishable except that they may have different internal properties such as [[Scope]] . A script engine may detect cases when the differences in the [[Scope]] properties of two or more joined Function objects are not externally observable. In those cases, it could reuse the same Function object rather than making a set of joined Function objects. When this occurs, two or more Function objects are joined, they have the following special behaviors: Any time a noninternal property of an object O is created or set, the corresponding property is immediately also created or set with the same value and attributes in all objects joined with O. Any time a noninternal property of an object O is deleted, the corresponding property is immediately also deleted in all objects joined with O. If objects O and P are joined, they compare as == and === to each other. Joining is transitive and symmetric, so that if objects O and P are joined and objects P and Q are joined, then objects O and Q are also automatically joined. 6.9.3 Function ObjectsEvery function instance has a [[Call]] property (referencing the method's body), a [[Construct]] property (referencing the function object's constructor), and a [[Scope]] property. The value of the [[Class]] property is the string "Function".
6.9.3.1 Constructing Function ObjectsA Function object is constructed using a parameter list specified by FormalParameterList of a function, a body specified by FunctionBody , and a scope chain specified by the [[Scope]] property. The value of the internal [[Prototype]] property of the Function constructor is the Function prototype object. In addition to the internal properties and the length property (whose value is 1), the Function constructor has the Function.prototype (i.e., the [[Prototype]]) property. The initial value of Function.prototype is the Function prototype object. When Function is called as part of a new expression, it is a constructor: it initializes the newly created object. A [[Prototype]] property is automatically created for every function, to provide for the possibility that the function is used as a constructor. When new Function (p1, p2, , pn, body) is invoked, the last argument specifies the body (executable code) of a function; any preceding arguments specify formal parameters. It is permissible but not necessary to have one argument for each formal parameter to be specified. For example, all three of the expressions new Function("a", "b", "c", "return a+b+c") , new Function("a, b, c", "return a+b+c") , and new Function("a,b", "c", "return a+b+c") produce the same result. When Function is called as a function rather than as a constructor, it creates and initializes a new Function object. The call Function(...) is equivalent to the object creation expression new Function(...) with the same arguments. When called with some arguments p 1 , p 2 , ... , p n body (where n might be 0, that is, there are no p arguments, and where body might also not be provided), the implementation creates and returns a new Function object as if the function constructor had been called with the same arguments. 6.9.3.2 Function Prototype ObjectThe [[Prototype]] property, referring to a function prototype object, is automatically created for every function, to allow for the possibility that the function is used as a constructor. The function prototype object is itself a Function object (its [[Class]] evaluates to the string "Function") that, when invoked, accepts any arguments and returns Undefined . The value of the internal [[Prototype]] property of the Function prototype object is the Object prototype object. It is a function with an empty body; if it is invoked, it merely returns Undefined . The Function prototype object does not have a valueOf property of its own; however, it inherits the valueOf property from the Object prototype object. The first step in the function construction process allows optimizing the common case of a function A that has a nested function B where B is not dependent on A. In this case the script engine may reuse the same object for B instead of creating a new one every time A is called. In the case of the code in Example 6.7 a script engine is allowed to join b1 and b2. In fact, it may make b1 and b2 the same object because there is no way to detect the difference between their [[Scope]] properties. On the other hand, a script engine does not join b3 and b4 because their source codes are not equal (recall the earlier definition of joined objects). A script engine does not join b5 and b6 because they were produced by two different calls to eval() and therefore their source codes are not equated. In general, it is likely to be useful to join two Function objects only in the cases where a script engine can prove that the differences between their [[Scope]] properties are not observable, so one object can be reused. Example 6.7 Object join scenario.function A() { function B(x) {return x*x;} return B; } function C() { return eval("(function (x) {return x*x;})"); } var b1 = A(); var b2 = A(); function b3(x) {return x*x;} function b4(x) {return x*x;} var b5 = C(); var b6 = C(); The following are the methods of the Function object:
6.9.4 Array ObjectsArray objects give special treatment to a certain class of property names . A property name P (in the form of a string value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2 32 “1. Every Array object has a length property whose value is always a nonnegative integer less than 2 32 . The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to properties of the Array object itself and is unaffected by length or index properties that may be inherited from its prototype. 6.9.4.1 The Array ConstructorWhen Array is called as part of a new expression, it is a constructor: it initializes the newly created object. When the Array function Array ( [ item 1 [ , item 2 [ , ] ] ] ) is called, create and return a new Array object exactly as if the Array constructor had been called with the same arguments. When Array is called as a function rather than as a constructor, it creates and initializes a new Array object. Thus the function call Array( ) is equivalent to the object creation expression new Array( ) with the same arguments. When new Array([item0[,item 1 [, ]]]) is called, the following property assignment occurs: The [[Prototype]] property of the newly constructed object is set to the original Array prototype object, the one that is the initial value of Array.prototype . The [[Class]] property of the newly constructed object is set to the string "Array". The length property of the newly constructed object is set to the number of arguments. When new Array(len) is called, the following property assignments occur: The [[Prototype]] property of the newly constructed object is set to the original Array prototype object, the one that is the initial value of Array.prototype . The [[Class]] property of the newly constructed object is set to Array . If the argument len is a Number and ToUint32(len) is equal to len, then the length property of the newly constructed object is set to ToUint32(len) . If the argument len is a Number and ToUint32(len) is not equal to len, a RangeError exception is thrown. If the argument len is not a Number , then the length property of the newly constructed object is set to 1 and the 0 property of the newly constructed object is set to len. 6.9.4.2 Properties of the Array Prototype ObjectThe value of the internal [[Prototype]] property of the Array prototype object is the Object prototype object. The Array prototype object does not have a valueOf property of its own; however, it inherits the valueOf property from the Object prototype object. The following are the methods of the Array object:
6.9.4.3 Properties of Array InstancesArray instances inherit properties from the Array prototype object. Array objects use a variation of the [[Put]] method used for other native ECMA Script objects. Assume A is an Array object and P is a string. The length property of this Array object is always numerically greater than the name of every property whose name is an Array index. 6.9.5 String Objects6.9.5.1 Properties of String InstancesString instances inherit properties from the String prototype object and also have a [[Value]] property and a length property. The [[Value]] property is the string value represented by this String object. The length property specified the number of characters in the String . Once a String object is created, this property is unchanging. 6.9.5.2 The String ConstructorWhen String is called as part of a new expression, it is a constructor that initializes the newly created object. On invocation of new String([value]) , the [[Prototype]] property is set to the original String prototype object, the one that is the initial value of String.prototype . The [[Class]] property of the newly constructed object is set to the string "String". The [[Value]] property of the newly constructed object is set to ToString(value) , or to the empty string if value is not supplied. When String is called as a function rather than as a constructor, it performs a type conversion. String([value]) returns a string value (not a String object) computed by ToString(value) . If value is not supplied, the empty string "" is returned. The value of the internal [[Prototype]] property of the String constructor is the Function prototype object. In addition to the internal properties and the length property (whose value is 1), the String constructor has the following properties:
6.9.5.3 String Prototype ObjectThe String prototype object is itself a String object whose value is an empty string and its [[Class]] property is assigned the string "String". The value of the internal [[Prototype]] property of the String prototype object is the Object prototype object.
6.9.6 Date ObjectA Date object contains a number indicating a particular instant in time to within a millisecond. 6.9.6.1 The Date ConstructorAs all object constructors, the Date constructor could be called as a function. When Date is called as a function rather than as a constructor, it returns a string representing the current time (UTC). The function call Date( ) is not equivalent to the object creation expression new Date( ) with the same arguments. When Date([year [, month [, date [, hours [, minutes [, seconds [, ms ]]]]]]]) is called, all of the arguments are optional; any arguments supplied are accepted but are completely ignored. A string is created and returned as if by the expression (new Date()).toString() . When Date is called as part of a new expression, it is a constructor: it initializes the newly created object. When new Date(year, month [, date [, hours [, minutes [, seconds [, ms ]]]]]) is called with two to seven arguments, the constructor computes the date from year , month , and (optionally) date , hours , minutes , seconds , and ms . The [[Prototype]] property of the newly constructed object is set to the original Date prototype object, the one that is the initial value of Date.prototype . The [[Class]] property of the newly constructed object is set to Date . When new Date() is invoked with no arguments, the [[Prototype]] property of the newly constructed object is set to the original Date prototype object, the one that is the initial value of Date.prototype . The [[Class]] property of the newly constructed object is set to Date . The [[Value]] property of the newly constructed object is set to the current time (UTC). 6.9.6.2 TimeTime is measured in ECMA Script in milliseconds since 01 January, 1970 UTC. Leap seconds are ignored. It is assumed that there are exactly 86,400,000 milliseconds per day. ECMAScript number values can represent all integers from “9,007,199,254,740,991 to 9,007,199,254,740,991; this range suffices to measure times to millisecond precision for any instant that is within approximately 285,616 years , either forward or backward, from 01 January, 1970 UTC. The actual range of times supported by ECMAScript Date objects is slightly smaller: exactly “100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC. This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC. The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0. 6.9.6.3 Day Number and Time within DayA given time value t belongs to day number Day(t) = floor(t/msPerDay) where the number of milliseconds per day is msPerDay = 86400000. The remainder is called the time within the day: TimeWithinDay(t) = t modulo msPerDay 6.9.6.4 Year NumberECMA Script uses an extrapolated Gregorian system to map a day number to a year number and to determine the month and date within that year. In this system, leap years are precisely those which are (divisible by 4) and ((not divisible by 100) or (divisible by 400)). All nonleap years are assumed to have 365 days with the usual number of days per month and leap years have an extra day in February. The day number of the first day of year y is given by: DayFromYear(y) = 365 x (y-1970) + floor((y-1969)/4) - floor((y-1901)/100) + floor((y-1601)/400) . The time value of the start of a year is TimeFromYear(y) = msPerDay x DayFromYear(y) , and a time value determines a year by YearFromTime(t) = the largest integer y ( closest to positive infinity) such that TimeFromYear(y) = t . 6.9.6.5 Month NumberMonths are identified by an integer in the range 0 to 11, inclusive. The mapping MonthFromTime(t) from a time value t to a month number is defined by Table 6.24 where DayWith inYear(t) = Day(t)-DayFromYear(YearFromTime(t)). Note that MonthFromTime(0) = 0 , corresponds to Thursday, 01 January, 1970. Table 6.24. Value of MonthFromTime(t)
A month value of 0 specifies January; 1 specifies February; 2 specifies March; 3 specifies April; 4 specifies May; 5 specifies June; 6 specifies July; 7 specifies August; 8 specifies September; 9 specifies October; 10 specifies November; and 11 specifies December. 6.9.6.6 Date NumberA date number is identified by an integer in the range 1 through 31, inclusive. The mapping DateFromTime(t) from a time value t to a month number is defined by Table 6.25. Table 6.25. Value of DateFromTime(t).
6.9.6.7 Week DayThe weekday for a particular time value t is defined as WeekDay(t)=(Day(t)+4)modulo 7 . A weekday value of 0 specifies Sunday; 1 specifies Monday; 2 specifies Tuesday; 3 specifies Wednesday; 4 specifies Thursday; 5 specifies Friday; and 6 specifies Saturday. Note that WeekDay(0) = 4 , corresponds to Thursday, 01 January, 1970. 6.9.6.8 Local Time Zone AdjustmentThe local time zone adjustment is a value LocalTZA measured in milliseconds which when added to UTC represents the local standard time. Daylight saving time is not reflected by LocalTZA . The value LocalTZA does not vary with time but depends only on the geographic location. 6.9.6.9 Daylight Saving Time AdjustmentThe algorithm to determine the daylight saving time adjustment DaylightSavingTA(t) , measured in milliseconds, must depend only on four items: (a) the time since the beginning of the year t “TimeFromYear(YearFromTime(t)), (b) whether t is in a leap year, (c) the weekday of the beginning of the year WeekDay(TimeFromYear(YearFromTime(t)) , and (d) the geographic location. An attempt is made to determine whether the exact time was subject to daylight saving time, but just whether daylight saving time would have been in effect if the current daylight saving time algorithm had been used at the time. This avoids complications such as taking into account the years that the locale observed daylight saving time year round. If the host environment provides functionality for determining daylight saving time, the implementation of ECMA Script is free to map the year in question to an equivalent year (same leap year-ness and same starting week day for the year) for which the host environment provides daylight saving time information. The only restriction is that all equivalent years should produce the same result. 6.9.6.10 Local Time ConversionsConversion from UTC to local time is defined by LocalTime(t) = t + LocalTZA + DaylightSavingTA(t) . Conversion from local time to UTC is defined by UTC(t)=t “ LocalTZA “DaylightSavingTA(t “LocalTZA) . Note that UTC(LocalTime(t)) is not necessarily always equal to t . 6.9.6.11 Hours, Minutes, Second, and MillisecondsThe following functions are useful in decomposing time values: HourFromTime(t) = floor(t / msPerHour) modulo HoursPerDay MinFromTime(t) = floor(t / msPerMinute) modulo MinutesPerHour SecFromTime(t) = floor(t / msPerSecond) modulo SecondsPerMinute msFromTime(t) = t modulo msPerSecond where HoursPerDay = 24, MinutesPerHour = 60, SecondsPerMinute = 60, msPerSecond = 1000, msPerMinute = msPerSecond x SecondsPerMinute = 60000, msPerHour = msPerMinute x MinutesPerHour = 3600000 . 6.9.6.12 Make Operators
|