Last active
September 13, 2019 15:54
-
-
Save jonathantneal/7e91ca129cece1e6adebd69babb70601 to your computer and use it in GitHub Desktop.
_define: Return an object with a property defined in a class prototype manner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 209/154 byte/gzip | |
/** | |
* Return an object with a property defined in a class prototype manner | |
* @param {T} object object on which to define the property | |
* @param {String|Symbol} key name or symbol to be defined | |
* @param {Descriptor} proto descriptor for the property being defined | |
* @template T | |
* @return {T} | |
*/ | |
function _define(object, key, proto) { | |
var descriptor = { configurable: 'configurable' in proto ? proto.configurable : true }; | |
if ('value' in proto) { | |
descriptor.value = proto; | |
descriptor.writable = 'writable' in proto ? proto.writable : true; | |
} else { | |
descriptor.get = proto.get; | |
descriptor.set = proto.set; | |
} | |
return Object.defineProperty(object, key, descriptor); | |
} | |
/** | |
* @typedef {Object} Descriptor | |
* @property {boolean} [configurable] whether the property may be changed or deleted | |
* @property {boolean} [enumerable] whether the property may be enumerated | |
* @property {function} [get] getter for the property which returns the computed value | |
* @property {function} [set] setter for the property | |
* @property {any} [value] value associated with the property | |
* @property {boolean} [writable] whether the property may be assigned | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 227/163 byte/gzip | |
/** | |
* Return an object with a property defined in a class prototype manner | |
* @param {T} object object on which to define the property | |
* @param {String|Symbol} key name or symbol to be defined | |
* @param {Descriptor} proto descriptor for the property being defined | |
* @template T | |
* @return {T} | |
*/ | |
function _define(object, key, proto) { | |
var descriptor = { configurable: 'configurable' in proto ? proto.configurable : true }; | |
if ('get' in proto || 'set' in proto) { | |
descriptor.get = proto.get; | |
descriptor.set = proto.set; | |
} else { | |
descriptor.value = proto.value || proto; | |
descriptor.writable = 'writable' in proto ? proto.writable : true; | |
} | |
return Object.defineProperty(object, key, descriptor); | |
} | |
/** | |
* @typedef {Object} Descriptor | |
* @property {boolean} [configurable] whether the property may be changed or deleted | |
* @property {boolean} [enumerable] whether the property may be enumerated | |
* @property {function} [get] getter for the property which returns the computed value | |
* @property {function} [set] setter for the property | |
* @property {any} [value] value associated with the property | |
* @property {boolean} [writable] whether the property may be assigned | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// _define.js: | |
test = _define( | |
_define( | |
{}, | |
'foo', { value: function () { return true } } | |
), | |
'bar', { get: function () { return true } } | |
); | |
console.log({ | |
isFooConfigurable: Object.getOwnPropertyDescriptor(test, 'foo').configurable === true, | |
isFooWritable: Object.getOwnPropertyDescriptor(test, 'foo').writable === true, | |
isFooNotEnumerable: Object.getOwnPropertyDescriptor(test, 'foo').enumerable === false, | |
isFooInvokedTrue: test.foo(), | |
isBarConfigurable: Object.getOwnPropertyDescriptor(test, 'bar').configurable === true, | |
isBarSansWritable: 'writable' in Object.getOwnPropertyDescriptor(test, 'bar') === false, | |
isBarNotEnumerable: Object.getOwnPropertyDescriptor(test, 'bar').enumerable === false, | |
isBarFalse: test.bar | |
}); | |
// _define.sugar.js: | |
test = _define( | |
_define( | |
{}, | |
'foo', function () { return true } | |
), | |
'bar', { get: function () { return true } } | |
); | |
console.log({ | |
isFooConfigurable: Object.getOwnPropertyDescriptor(test, 'foo').configurable === true, | |
isFooWritable: Object.getOwnPropertyDescriptor(test, 'foo').writable === true, | |
isFooNotEnumerable: Object.getOwnPropertyDescriptor(test, 'foo').enumerable === false, | |
isFooInvokedTrue: test.foo(), | |
isBarConfigurable: Object.getOwnPropertyDescriptor(test, 'bar').configurable === true, | |
isBarSansWritable: 'writable' in Object.getOwnPropertyDescriptor(test, 'bar') === false, | |
isBarNotEnumerable: Object.getOwnPropertyDescriptor(test, 'bar').enumerable === false, | |
isBarFalse: test.bar | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment