Object.create(proto, props)
ma dwa argumenty:
proto
— the object which should be the prototype of the newly-created object.
props
(optional) — an object whose properties specify property descriptors to be added to the newly-created object, with the corresponding property names.
Format obiektu props
jest zdefiniowane here.
W skrócie, dostępne opcje dla każdego deskryptora własności są takie:
{
configurable: false, // or true
enumerable: false, // or true
value: undefined, // or any other value
writable: false, // or true
get: function() { /* return some value here */ },
set: function (newValue) { /* set the new value of the property */ }
}
Problem z kodem jest to, że deskryptory własności już zdefiniowane nie są obiektami.
Oto przykład prawidłowego wykorzystania deskryptorów własności:
var test = Object.create(null, {
ex1: {
value: 1,
writable: true
},
ex2: {
value: 2,
writable: true
},
meth: {
get: function() {
return 'high';
}
},
meth1: {
get: function() {
return this.meth;
}
}
});
met zwraca wysoki - krzyknąłem. XD – evolutionxbox