8.10.4 FromPropertyDescriptor

2010-04-19

Object FromPropertyDescriptor(Desc:Descriptor)

This operation creates a new object using given Property Descriptor to instantiate certain properties. If data descriptor, [[Value]] and [[Writable]] will be set. If accessor descriptor [[Set]] and [[Get]] will be set. And regardless of descriptor type, [[Enumerable]] and [[Configurable]] will both be set. All according to the given Descriptor. The new object is returned.

The argument is assumed to be fully populated Property Descriptor.

Note that the second argument for DefineOwnProperty a Property Descriptor is. This is the literal notation convention used by the spec, which translates nicely to ECMAScript (albeit slightly ambiguous).

Code: (Abstract Ecma)
function FromPropertyDescriptor(Desc){
if (Desc === undefined) return Desc;
var obj = new Object();
if (IsDataDescriptor(Desc)) {
obj.[[DefineOwnProperty]]("value", {[[Value]]: Desc.[[Value]], [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, false);
obj.[[DefineOwnProperty]]("writable", {[[Value]]: Desc.[[Writable]], [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, false);
} else { // IsAccessorDescriptor(Desc) must be true because Desc is assumed to be fully populated. It cannot be generic.
obj.[[DefineOwnProperty]]("get", {[[Value]]: Desc.[[Get]], [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, false);
obj.[[DefineOwnProperty]]("set", {[[Value]]: Desc.[[Set]], [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, false);
}
obj.[[DefineOwnProperty]]("enumerable", {[[Value]]: Desc.[[Enumerable]], [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, false);
obj.[[DefineOwnProperty]]("writable", {[[Value]]: Desc.[[Writable]], [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, false);
return obj;
}