Skip to content

Instantly share code, notes, and snippets.

@shesek
Last active December 11, 2015 19:48
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 shesek/4650898 to your computer and use it in GitHub Desktop.
Save shesek/4650898 to your computer and use it in GitHub Desktop.
getter, setter and lazy
multi = (fn) -> (obj, name, a) ->
if a? then fn obj, name, a
else fn obj, name_, a_ for name_, a_ of name
def = (obj, name, prop) ->
prop.configurable ?= true
prop.enumerable ?= true
Object.defineProperty obj, name, prop
module.exports = exports =
etter: multi def
getter: multi (obj, name, get) -> def obj, name, { get }
setter: multi (obj, name, set) -> def obj, name, { set }
lazy: multi (obj, name, get) -> exports.getter obj, name, ->
value = get.call this
def this, name, { value, writable: false }
value
{
"name": "etter",
"version": "0.1.1",
"main": "etter.coffee",
"repository": {
"type": "git",
"url": "git://gist.github.com/4650898.git"
},
"license": "BSD"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment