You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below is a possible API for this helper function which prioritizes the shortest possible calling signature over compexity + length of the helper function. In any app which is of above trivial size, there will be a lot of calls to this function, so that should lead to less bytes in total.
Set object properties
Helper function defineProperties() would be called as follows:
// Shortcut form where all descriptors `true`defineProperties(obj,{x: 123,y: 456});// Long form where all descriptors `false` - 2nd array item is bitmapdefineProperties(obj,{x: [123,7],y: [456,7]});// Array value - Disambiguated by wrapping value in another arraydefineProperties(obj,{x: [[1,2,3]]});// Getter/setter - Disambiguated by 2nd array item being non-integerdefineProperties(obj,{x: [functiongetter(){},functionsetter(){}]});// Getter/setter with all descriptors `false` - 3rd array item is bitmapdefineProperties(obj,{x: [functiongetter(){},functionsetter(){},7]});// Getter only - Disambiguated by 2nd array item not being numberdefineProperties(obj,{x: [functiongetter(){},,]});// Setter only - Disambiguated by 2nd array item not being numberdefineProperties(obj,{x: [,functionsetter(){}]});// Property name is '__proto__' - signalled by 4th bit of bitmapdefineProperties(obj,{a: ['proto',8]});
defineProperties() would return same object so it's chainable.
No need to also provide a defineProperty() method for defining a single property as d(o,{x:123}) is same number of chars as d(o,'x',123).
Create object with prototype
1st arg 1 means create new Object with Object.create() using 2nd arg as prototype and 3rd arg as object props:
// Object with prototype `Klass.prototype` and no propertiesdefineProperties(1,Klass.prototype)// Object with prototype `Klass.prototype` with propertiesdefineProperties(1,Klass.prototype,{x: 123,y: 456})
Only 0 or 1 args means create Object with null prototype, using 1st arg as object props:
// `null` prototype object with no propertiesdefineProperties()// `null` prototype object with propertiesdefineProperties({x: 123,y: 456})
Set prototype
3 args where 1st arg is Object means set prototype as 3rd arg:
// Just set prototypedefineProperties(obj,0,Klass.prototype)// Define props at same timedefineProperties(obj,{x: 123,y: 456},Klass.prototype)
1st arg with value 2 means set prototype to null:
// Just set prototypedefineProperties(2,obj)// Define props at same timedefineProperties(2,obj,{x: 123,y: 456})
Simple cases
For really simple cases, could just use direct assignments as they're shorter i.e. o.a=1 is shorter than d(o,{a:1}). The functional form gets shorter only with 4 props or more (not including the helper function itself).
Actually, it's unnecessary to overload this function to also replace Object.create() and Object.setPrototypeOf(). They can be combined with it using defineProps( Object.create( null ), { /* ... */ } ) / Object.setPrototypeOf( defineProps( { /* ... */ } ) , proto ).
Only overload should be where base is an empty object, in which case first arg can be omitted. defineProperties( { x: [ 1, 7 ] } ) is equivalent to defineProperties( {}, { x: [ 1, 7 ] } ).
There's also Object.freeze(), Object.seal() and Object.preventExtensions() to consider - again these should be used separately rather than bloating defineProps() with this functionality.
Should have a separate helper for deleting properties.
Also need to deal with where intent is not to overwrite value, only set descriptors. This can be indicated with single-item definition array e.g. defineProperties( [ 1, 2, 3 ], { 0: [ 7 ] } ) sets all property descriptors for first array item to false but leaves its value as 1.
Livepack's output often includes a lot of verbose
Object.defineProperties()
calls like:Could instead use a runtime helper function which can be reused.
Aims
Ideally it would:
Object.assign()
,Object.defineProperty()
andObject.defineProperties()
{a:1}
is 2 chars shorter than['a',1]
).__proto__
.writable
,enumerable
,configurable
.true
.Object.create()
.Object.setPrototypeOf()
.Implementation
Below is a possible API for this helper function which prioritizes the shortest possible calling signature over compexity + length of the helper function. In any app which is of above trivial size, there will be a lot of calls to this function, so that should lead to less bytes in total.
Set object properties
Helper function
defineProperties()
would be called as follows:defineProperties()
would return same object so it's chainable.No need to also provide a
defineProperty()
method for defining a single property asd(o,{x:123})
is same number of chars asd(o,'x',123)
.Create object with prototype
1st arg
1
means create new Object withObject.create()
using 2nd arg as prototype and 3rd arg as object props:Only 0 or 1 args means create Object with
null
prototype, using 1st arg as object props:Set prototype
3 args where 1st arg is Object means set prototype as 3rd arg:
1st arg with value
2
means set prototype tonull
:Simple cases
For really simple cases, could just use direct assignments as they're shorter i.e.
o.a=1
is shorter thand(o,{a:1})
. The functional form gets shorter only with 4 props or more (not including the helper function itself).The text was updated successfully, but these errors were encountered: