Skip to content

Instantly share code, notes, and snippets.

@shesek
Created February 26, 2012 13:45
Show Gist options
  • Save shesek/1916895 to your computer and use it in GitHub Desktop.
Save shesek/1916895 to your computer and use it in GitHub Desktop.
ES5 Object.create helpers
defaults = writable: true, enumerable: true, configurable: true
initializer = (key) -> (val) ->
prop = Object.create defaults
prop[key] = val
prop
value = initializer 'value'
getter = initializer 'get'
setter = initializer 'set'
modifier = (attrs) -> (prop) -> prop[key]=value for key,value of attrs; prop
hidden = modifier enumerable: false
readonly = modifier writable: false
frozen = modifier configurable: false, writable: false
test = Object.create null,
a: value 100
b: getter -> @a
c: setter -> @b
d: frozen value 200
e: frozen hidden value 300
e: hidden readonly getter -> @x
@shesek
Copy link
Author

shesek commented Feb 26, 2012

Just a fun idea I had, trying to find a better way to do something that was posted on IRC without creating separate functions for every combination of attributes.

@shesek
Copy link
Author

shesek commented Feb 26, 2012

Another possibility:

compose = (fns...) -> (val) -> val = fn val for fn in fns; val

modifier = (key, value) -> (prop) -> prop[key]=value; prop

hidden = modifier 'enumerable', false
readonly = modifier 'writable', false
noconf = modifier 'configurable', false
frozen = compose readonly, noconf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment