Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getters and setters #451

Closed
weepy opened this issue Jun 22, 2010 · 3 comments
Closed

getters and setters #451

weepy opened this issue Jun 22, 2010 · 3 comments

Comments

@weepy
Copy link

weepy commented Jun 22, 2010

any plans to allow a coffee version of the following:

`get isEmpty() { return this.length === 0 }`
@jashkenas
Copy link
Owner

Nope, not until it's actually implemented across runtimes. Here's the previous tickets on the issue:

#64, #322

@adrusi
Copy link

adrusi commented Feb 22, 2011

but a lot of coffeescript development is command line, so I'd guess at least 2/3 of coffeescript run is run in an environment that supports them

@JHawkley
Copy link

Although it would be nice to have a proper getter/setter syntax for CoffeeScript, especially now that Object.defineProperty is pretty much supported across all major browsers and Node.js, you can use language features of CoffeeScript to make some pretty elegant getters and setters without having to alter the language. Observe this pattern I've been experimenting with:

Function::define = (prop, desc) ->
    Object.defineProperty this.prototype, prop, desc

class GetterSetterTest
    constructor: (@_obj = {}) ->

    # 'obj' is defined via the prototype, the definition proxied through
    # to 'Object.defineProperty' via a function called 'define' providing
    # some nice syntactic sugar.  Remember, the value of '@' is
    # GetterSetterTest itself when used in the body of it's class definition.
    @define 'obj'
        get: ->
            return @_obj
        set: (value) ->
            @_obj = value

    _obj: null

Only drawback is the use of the single-quotes when defining the property, but the result almost looks like it is a part of the language standard. The define function can also be modified to make use of __defineGetter__ and __defineSetter__ for backward compatibility (just look out for IE8's screwy implementation of Object.defineProperty). If you're planning on targeting only fairly modern browsers for your application and feel you need getter/setters, this may be a good replacement in the mean time.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants