Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Last active September 13, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/7e91ca129cece1e6adebd69babb70601 to your computer and use it in GitHub Desktop.
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
// 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
*/
// 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
*/
// _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