Ecmascript 5
// JavaScript - Ecmascript 5
Ecmascript 4 was completely abandoned. Ecmascript 5 was released in 2009.
1. "use strict";
1. No more implied global variables within functions.
2. apply and call do not default to the global object.
3. No with statement.
4. Restriction on eval.
5. eval and arguments are reserved.
6. No more arguments.caller or argument.callee.
7. No more octal literals
8. Duplicate names in an object literal or function parameters
results in a syntax error.
2. parseInt() default to radix 10
3. JSON.parse() and JSON.stringify()
4. Function.prototype.bind
5. String.prototype.trim
6. Additions to Array:
1. every
2. filter
3. forEach
4. indexOf
5. lastIndexOf
6. map
7. reduce
8. reduceRight
9. some
a. isArray
7. Date.now()
8. Date.prototype.toISOString
9. new Date(string) and Date.parse(string) will try ISO format first
10. Additions to Object:
1. keys()
2. create()
3. defineProperty
4. defineProperties
5. getOwnPropertyDescriptor()
6. getOwnPropertyNames(obj)
7. getPrototypeOf(obj)
8. seal
9. freeze
a. preventExtensions()
b. isSealed()
c. isFrozen()
d. isExtendable()
11. We can now use reserved keywords (such as new and function) after
the dot operator and as unquoted property keys in object literals such as:
var obj = { new: 'abc' };
obj.new // 'abc'
Prior to ES5, we can not use reserved keywords like this.
12. Trailing commas in object literals and array literals are now legal.
13. Multiline string literals: String literals can now span multiple lines if we
escape the end of the line via a backslash.
14. Ability to access characters within a string using the bracket operator [...].
Previously, we have to use the charAt function.
15. Some built-in objects now have special toJSON() method:
Boolean.prototype.toJSON()
Number.prototype.toJSON()
String.prototype.toJSON()
Date.prototype.toJSON()
// JavaScript - Ecmascript 5 - "use strict":
The effect of "use strict":
1. No more implied global variables within functions.
function foo() {
someVarName = 1; // normally creates a global variable, error in strict
}
2. The with() statement is forbidden
3. Restrictions for eval() and arguments.
1. Almost every usage of eval() results in an error.
2. The arguments property of a method may not be accessed
function foo() {
bar.arguments = [1, 2, 3]; // cause error in strict mode
}
4. Unique parameter and object literal properties names
function identicalNames(param, param) { // error in strict mode
var obj = { foo: "bar", foo: "foo"}; // erroe in strict mode
}
5. No octal number
function octal() {
alert(42 == 052); // normal print true, but error in strict mode
}
It is not recommended to enable strict mode globally in your project because
this would affect included 3rd party libraries. One solution would be to
include "use strict" in every single method, but this would be very tedious
and would presumably be forgotten once or twice in a big project.
If you follow Douglas Crockford's advice and use only a single global variable
as a kind of container for your whole application, you can use a self-invoking
anonymous function to apply strict mode to all of your methods without
polluting the global namespace and without forcing the strict mode for included
3rd party libraries:
var MYAPP = {};
(function() {
"use strict";
MYAPP.someFunction = function() {
someUndeclaredVariable = 42; // error
};
MYAPP.anotherFunction = function() {
console.log(42 == 052); // error
};
})();
// JavaScript - Ecmascript 5 - Property Descriptor Maps:
JavaScript allows us to dynamically add new properties to an object any time we
want and change or delete existing ones. This may seem useful at times, but
surely is always a probable part to break a program. With the property
descriptor maps introduced in ES5, we can now specify how the properties of
our object can be altered after creation.
var obj = {};
Object.defineProperty(obj, ‘attributeName’, {
value: 1,
writable: true,
enumerable: true,
configurable: true
});
The meaning for the attributes mentioned above:
1. Value: The intrinsic value of the property
2. Writable: can the value of the attribute be changed after being set?
3. Enumerable: Specifys if the property may be iterated on for example in
for-loops
4. Configurable: Specifys if a property can be deleted and how the values of
its property descriptor map can be modified.
// JavaScript - Ecmascript 5 - Getter and Setter methods:
An additional nice feature that comes with the property descriptor maps is the
ability to implement getter and setter like methods for the properties of your
objects. The “get” method will be called every time the property is accessed,
the “set” method every time the value of the property is changed.
Properties with a getter and setter are no normal object properties and may not
have an intrinsic value. You have to e.g. create a property “value” with a
getter and setter to manipulate the (private) property “_value”. With another
self-invoking anonymous function and thanks to closures you can create a private
field “_value” that can only be accessed through the getters and setters of
“value”.
var obj = {};
(function (){
var _value = 1;
Object.defineProperty(obj, "value", {
get : function(){
console.log("accessing _value");
return _value;
},
set : function(newValue){
console.log("changing _value to: " + newValue)
_value = newValue;
}}
);
})();
console.log(obj.value);
obj.value = 5;
console.log(obj.value);
// prints...
// accessing _value
// 1
// changing _value to: 5
// accessing _value
// 5
Another example:
Getters and setters allow you to implement the getting and setting of a
property via methods. For example, the following object obj contains a getter
for the property foo:
var obj = {
get foo() { return 'abc' }
};
obj.foo
// 'abc'
Notice the use of the get keyword, the function name, and how it is invoked (
just as though foo() is just a simple property, without the parentheses ).
// JavaScript - Ecmascript 5 - Object.preventExtensions():
Prohibits the ability to add new properties to an object:
var obj = {foo: "foo"};
obj.bar = "bar";
Object.preventExtensions(obj);
obj.someVar = "123"; // error
// JavaScript - Ecmascript 5 - Object.seal()
Sealed objects may not be extended with new properties and exisiting properties
may not be deleted.
// JavaScript - Ecmascript 5 - Object.freeze():
In addition to ‘sealed’ objects, freeze() also prohibits to change the values
of the property descriptor map of any property of the object, resulting in an
immutable object.
page revision: 9, last edited: 30 Dec 2016 16:55





