ECMAScript 5 / 5.1
Strict mode
Obiekty
Poniżej ES5 niektóre właściwości obiektu muszą być pisane jako string, jeśli nie to parser zwraca SyntaxError, np. { 'delete' : 'text' }
.
[ES5] Prototyp za pomocą Object.create()
Metoda Object.create()
jest dostępna od ES5.
var myObj = {
myString: "Hello world!"
};
var myPrototype = {
meaningOfLife: 42,
myFunc: function(){
return this.myString.toLowerCase()
}
};
var myObj = Object.create(myPrototype);
myObj.meaningOfLife; // = 42
// This works for functions, too.
myObj.myFunc(); // = "hello world!"
Istnieje jednak polyfill dla tej metody.
if (Object.create === undefined){ // don't overwrite it if it exists
Object.create = function(proto){
// make a temporary constructor with the right prototype
var Constructor = function(){};
Constructor.prototype = proto;
// then use it to create a new, appropriately-prototyped object
return new Constructor();
}
}
[ES5] Metoda Object.keys()
Zwraca wszystkie klucze właściwości obiektu. Jest to ulepszona wersja metody getOwnPropertyNames()
, która to wypisywała również właściwość constructor
.
[ES5] Definiowanie konstruktora obiektu
Poniższy kod zadziała tylko w ES5.
Object.defineProperty(Person.prototype, "constructor", {
enumerable: false,
value: Person
});
[ES5] Metoda Object.getPrototypeOf()
Jest alternatywą dla metody isPrototypeOf()
.