var statementsArray
String
String.prototype.anchor(name)String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.fixed()String.prototype.fontcolor(color)String.prototype.fontsize(size)String.prototype.italics()String.prototype.link(href)String.prototype.small()String.prototype.strike()String.prototype.sub()String.prototype.substr(start, length)String.prototype.sup()Date
RegExp
Function
Object
This specification aims to document the differences between the ECMAScript specification and the compatibility and interoperability requirements for ECMAScript implementations in web browsers. [ECMASCRIPT]
Everything in this specification is normative except for examples, notes, and sections marked as non-normative.
A JavaScript implementation must be a conforming implementation of ECMAScript with the extensions described in this document. [ECMASCRIPT]
A user agent must also be a conforming implementation of HTML, if applicable. [HTML]
The key words “must” and “should” in this document are to be interpreted as described in RFC 2119. [RFC2119]
Special symbols in this document are identified using their Unicode code points and names. [UNICODE]
When this specification uses the term ECMAScript character, it means a 16-bit unsigned value used to represent a single 16-bit unit of text. [ECMASCRIPT]
To clarify the semantics of certain constructs used throughout this document, it is useful to define a set of abstract operations. These abstract operations are not a part of the language, and must not be exposed to Web authors; they are only defined here to aid the specification of the semantics of the language.
min(a, b)The abstract operation min(a, b) returns the smallest of its two arguments, as per the following algorithm:
max(a, b)The abstract operation max(a, b) returns the greatest of its two arguments, as per the following algorithm:
CheckObjectCoercible(value)The abstract operation CheckObjectCoercible(value) throws an error if its argument is a value that cannot be converted to an object. It is defined in the ECMAScript specification.
EscapeAttributeValue(value)The abstract operation EscapeAttributeValue(value) produces a string suitable for use in an HTML attribute value wrapped in double quotes. [HTML] This is done according to the following algorithm:
"” (U+0026 U+0071 U+0075 U+006F U+0074 U+003B).
For security reasons, Web authors should not rely on any such escaping, as older, non-JavaScript-compliant implementations might not perform this operation correctly.
IsCallable(value)The abstract operation IsCallable(value) determines if its argument is a callable function object. It is defined in the ECMAScript specification.
ToInteger(value)The abstract operation ToInteger(value) converts its argument to an integral numeric value (of type Number). It is defined in the ECMAScript specification.
ToString(value)The abstract operation ToString(value) converts its argument to a value of type String. It is defined in the ECMAScript specification.
ToHTML(tagName, content, attributeName, attributeValue)The abstract operation ToHTML(tagName, content, attributeName, attributeValue) produces an HTML string, according to the following algorithm:
<” (U+003C), tagName, “>” (U+003E), content, “</” (U+003C U+002F), tagName, and “>” (U+003E).
<” (U+003C), tagName, “ ” (U+0020), attributeName, “="” (U+003D U+0022), EscapeAttributeValue(attributeValue), “">” (U+0022 U+003E), content, “</” (U+003C U+002F), tagName, and “>” (U+003E).
JavaScript implementations must fully implement Annex B of the ECMAScript specification. This includes:
escape(string) and unescape(string) methods;
String.prototype.substr(start, length) method;
Date.prototype.getYear(), Date.prototype.setYear(year), and Date.prototype.toGMTString() methods.
For optimal interoperability, JavaScript implementations should use the latest available Unicode database to determine which ECMAScript characters are allowed in Identifiers and IdentifierNames, and which ECMAScript characters are whitespace characters. [UNICODE]
Tests are available: http://mathias.html5.org/tests/javascript/comment-syntax/.
<!-- must be treated as the start of a SingleLineComment — equivalent to //.
var x = true; <!-- x = false; // note: no syntax error x; // true
--> at the start of a line, optionally preceded by whitespace or single-line MultiLineComments, must be treated as a SingleLineComment — equivalent to //.
var x = true; --> x = false; // note: no syntax error x; // true
var statementsThe erratum in https://bugs.ecmascript.org/show_bug.cgi?id=78#c0 must be followed so that var statements at the top level of scripts can shadow any properties from the global object’s prototype chain.
ArrayTests are available: http://mathias.html5.org/tests/javascript/array/.
Array.prototype.splice(start, deleteCount, item1, item2, …)This method is defined in the ECMAScript specification. However, for Web compatibility, JavaScript implementations must perform the following additional step before continuing with the algorithm:
arguments.length == 1), set the value of deleteCount to Infinity.
According to the algorithm in the ECMAScript spec, omitting the deleteCount argument or setting its value to undefined would have the same effect as passing the value 0 (rather than Infinity), regardless of whether the start argument is provided or not.
var array = ['a', 'b', 'c', 'd']; array.splice(2); // ['c', 'd'] array; // ['a', 'b'] array = ['a', 'b', 'c', 'd']; array.splice(2, Infinity); // ['c', 'd'] array; // ['a', 'b'] array = ['a', 'b', 'c', 'd']; array.splice(2, undefined); // [] array; // ['a', 'b', 'c', 'd']
StringTests are available: http://mathias.html5.org/tests/javascript/string/.
Section 15.5.4 of the ECMAScript 5.1 specification defines the properties of the String.prototype object. For Web compatibility, user agents must extend this object with the following extra methods.
These methods are all generic, meaning the this argument need not be a String object.
String.prototype.anchor(name)CheckObjectCoercible(this).
ToString(this).
ToString(name).
ToHTML("a", content, "name", attributeValue).
The length property of the anchor method is 1.
The use of this method is not recommended for Web authors; DOM APIs should be used instead. [DOM] [HTML] However, if this method is used nevertheless, the characters & and " in the value of the name argument should be escaped as “&” (U+0026 U+0061 U+006D U+0070 U+003B) and “"” (U+0026 U+0071 U+0075 U+006F U+0074 U+003B), respectively.
String.prototype.big()CheckObjectCoercible(this).
ToString(this).
ToHTML("big", content).
The length property of the big method is 0.
String.prototype.blink()CheckObjectCoercible(this).
ToString(this).
ToHTML("blink", content).
The length property of the blink method is 0.
String.prototype.bold()CheckObjectCoercible(this).
ToString(this).
ToHTML("b", content).
The length property of the bold method is 0.
String.prototype.fixed()CheckObjectCoercible(this).
ToString(this).
ToHTML("tt", content).
The length property of the fixed method is 0.
String.prototype.fontcolor(color)CheckObjectCoercible(this).
ToString(this).
ToString(color).
ToHTML("font", content, "color", attributeValue).
The length property of the fontcolor method is 1.
The use of this method is not recommended for Web authors; DOM APIs should be used instead. [DOM] [HTML] However, if this method is used nevertheless, the characters & and " in the value of the color argument should be escaped as “&” (U+0026 U+0061 U+006D U+0070 U+003B) and “"” (U+0026 U+0071 U+0075 U+006F U+0074 U+003B), respectively.
String.prototype.fontsize(size)CheckObjectCoercible(this).
ToString(this).
ToString(size).
ToHTML("font", content, "size", attributeValue).
The length property of the fontsize method is 1.
The use of this method is not recommended for Web authors; DOM APIs should be used instead. [DOM] [HTML] However, if this method is used nevertheless, the characters & and " in the value of the size argument should be escaped as “&” (U+0026 U+0061 U+006D U+0070 U+003B) and “"” (U+0026 U+0071 U+0075 U+006F U+0074 U+003B), respectively.
String.prototype.italics()CheckObjectCoercible(this).
ToString(this).
ToHTML("i", content).
The length property of the italics method is 0.
String.prototype.link(href)CheckObjectCoercible(this).
ToString(this).
ToString(href).
ToHTML("a", content, "href", attributeValue).
The length property of the link method is 1.
The use of this method is not recommended for Web authors; DOM APIs should be used instead. [DOM] [HTML] However, if this method is used nevertheless, the characters & and " in the value of the href argument should be escaped as “&” (U+0026 U+0061 U+006D U+0070 U+003B) and “"” (U+0026 U+0071 U+0075 U+006F U+0074 U+003B), respectively.
String.prototype.small()CheckObjectCoercible(this).
ToString(this).
ToHTML("small", content).
The length property of the small method is 0.
String.prototype.strike()CheckObjectCoercible(this).
ToString(this).
ToHTML("strike", content).
The length property of the strike method is 0.
String.prototype.sub()CheckObjectCoercible(this).
ToString(this).
ToHTML("sub", content).
The length property of the sub method is 0.
String.prototype.substr(start, length)String.prototype.sup()CheckObjectCoercible(this).
ToString(this).
ToHTML("sup", content).
The length property of the sup method is 0.
DateDate.UTC(year, month, date, hours, minutes, seconds, ms)When called with fewer than two arguments, Date.UTC must return NaN.
RegExpAny DecimalEscapes in character class ranges must behave like \0 rather than throwing syntax errors.
For example, /[\1-Z]/ matches any character with a code point between U+0000 and U+005A.
The octal escape sequence syntax for string literals as described in Annex B of the ECMAScript spec must be supported, and must also apply to regular expression literals, even in strict mode code.
/\123/.test('S'); // true
RegExp.$1-$9After a regexp is executed the RegExp constructor object has properties $1...$9 which are assigned the values of the first 9 match groups from the previous regexp.
RegExp.lastMatch / RegExp["$&"]RegExp.prototype.compile()The length property of the compile method is 1.
FunctionTests are available: http://mathias.html5.org/tests/javascript/function/.
Function.prototype.argumentsEach Function instance must have an arguments property that is equivalent to the standard arguments object within that function.
var a = function(x, y, z) {
return [a.arguments, a.arguments === arguments];
};
a(1, 2, 3); // [{ '0': 1, '1': 2, '2': 3, 'length': 3 }], true]
The value of a function’s arguments property must be null if it’s accessed outside the function’s inner scope.
var a = function(x, y, z) {
return a.arguments;
};
a(1, 2, 3); // { '0': 1, '1': 2, '2': 3, 'length': 3 }
a.arguments; // null
The standard arguments object is not created if the identifier “arguments” occurs as an Identifier in the function’s FormalParameterList or occurs as an Identifier of a FunctionDeclaration contained in the function code. However, even in these cases, the arguments property must be added to the function’s prototype.
var b = function(x, y, arguments) {
return [b.arguments, b.arguments === arguments];
};
b(1, 2, 3); // [{ '0': 1, '1': 2, '2': 3, 'length': 3 }, false]
Strict mode functions must have a non-configurable accessor property with the name arguments that throws a TypeError exception on access.
var a = function(x, y, z) {
'use strict';
return a.arguments;
};
a(1, 2, 3); // throws a TypeError
Function.prototype.callerEach Function instance must have a caller property that is a reference to the function that invoked the specified function.
var a = function() {
return a.caller;
};
var b = function() {
return a() === b;
};
b(); // true
The value of a function’s caller property must be null if it’s accessed outside the function’s inner scope, or if the function was invoked by top-level code.
var a = function() {
return a.caller;
};
a.caller; // null
a(); // null
Strict mode functions must have a non-configurable accessor property with the name caller that throws a TypeError exception on access.
var fn = function(x, y, z) {
'use strict';
return fn.caller;
};
fn(); // throws a TypeError
Function.prototype.toString()ObjectTests are available: http://mathias.html5.org/tests/javascript/object/.
Object.prototype.__defineGetter__(propertyName, function)Every Object instance must have a __defineGetter__ method which binds a property of that object to a function that is called each time the property is accessed.
IsCallable(function) is false, throw a TypeError and return; else, continue following these steps.
ToString(propertyName).
[[Enumerable]] property of obj’s prop property to true.
__lookupGetter__ method of the obj object is called with an argument arg for which prop is equal to ToString(arg), return a reference to function.
undefined.
var object = {},
counter = 0;
object.__defineGetter__('foo', function() {
return ++counter;
});
[object.foo, object.foo, object.foo]; // [1, 2, 3]
var myDate = new Date;
myDate.__defineGetter__('year', function() {
return this.getFullYear();
});
myDate; // Sun Jul 01 2012 13:33:37 GMT+0200 (CEST)
myDate.year; // 2012
The length property of the __defineGetter__ method is 2.
Object.prototype.__defineSetter__(propertyName, function)Every Object instance must have a __defineSetter__ method which binds a property of that object to a function that is called each time the property is set.
IsCallable(function) is false, throw a TypeError and return; else, continue following these steps.
ToString(propertyName).
[[Enumerable]] property of obj’s prop property to true.
undefined.
__lookupSetter__ method of the obj object is called with an argument arg for which prop is equal to ToString(arg), return a reference to function.
undefined.
var object = {};
object.__defineSetter__('degrees', function(degrees) {
this.radians = degrees * Math.PI / 180;
});
object.degrees = 180;
object.degrees; // undefined
object.radians; // 3.141592653589793
var myDate = new Date;
myDate.__defineSetter__('year', function(y) {
this.setFullYear(y);
});
myDate; // Sun Jul 01 2012 13:33:37 GMT+0200 (CEST)
myDate.year = 1988;
myDate; // Fri Jul 01 1988 13:33:37 GMT+0200 (CEST)
The length property of the __defineSetter__ method is 2.
Object.prototype.__lookupGetter__(propertyName)Every Object instance must have a __lookupGetter__ method that implements the following algorithm:
ToString(propertyName).
__defineGetter__ or the get syntax in the object initializer, return the getter function; else, return undefined.
var object = { get foo() { return 1; } };
object.__lookupGetter__('foo'); // function foo() { return 1; }
The length property of the __lookupGetter__ method is 1.
Object.prototype.__lookupSetter__(propertyName)Every Object instance must have a __lookupSetter__ method that implements the following algorithm:
ToString(propertyName).
__defineSetter__ or the set syntax in the object initializer, return the setter function; else, return undefined.
var object = { set foo(value) { this.bar = value * 2; } };
object.__lookupSetter__('foo'); // function foo(value) { this.bar = value * 2; }
The length property of the __lookupSetter__ method is 1.
Object.prototype.__proto__All objects must have a mutable __proto__ property that is a reference to the prototype of the object. Note that ECMAScript defines that objects for which the [[Extensible]] internal property is false must not have their prototype mutated.
var string = 'foo'; string.__proto__ === String.prototype; // true
var object = {};
object.__proto__ === Object.prototype; // true
object.length; // undefined
object.__proto__ = Array.prototype;
object.length; // 0
In the case that setting an object’s __proto__ would cause a prototype chain to become cyclic, the setter must fail and throw an error.
var object = {};
object.__proto__ = object; // throws an Error
__proto__ will be fully specced in ECMAScript 6 (although the latest draft only defines it in Annex B). This section will be amended / removed when that happens.
Thanks to Anne van Kesteren, Aryeh Gregor, David Håsäther, James Graham, John-David Dalton, Ms2ger, Simon Pieters, Mark ‘Tarquin’ Wilton-Jones, and Tom Schuster for their useful comments.
Special thanks to James Graham for collecting and documenting most of these requirements on the WHATWG Wiki, and Juriy ‘kangax’ Zaytsev (Юрий Зайцев) for his ECMAScript Extensions Compatibility Table.