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

Add named classes that work in use strict. #2262

Closed
wants to merge 1 commit into from
Closed

Add named classes that work in use strict. #2262

wants to merge 1 commit into from

Conversation

paulmillr
Copy link

This will allow use of @constructor.__name__ for building great dynamic apps.

class Tweet
console.log Tweet.__name__
# => 'Tweet'

class View
  initialize: ->
    super
    templateName = underscorize(@constructor.__name__).replace(/_view$/, '')
    @template ?= require "./templates/#{templateName}"

class TweetView extends View

CoffeeScript 1.3.1 already uses @constructor.name for this purpose, but as we've seen, it breaks 'use strict' mode because @constructor.name is a read-only property.

Related: #494, #2052, #2249, #2250, f3a1f46.

@idflood
Copy link

idflood commented Apr 14, 2012

The patch looks good. Waiting for this fix to be applied since I extensively "use strict".

@vendethiel
Copy link
Collaborator

+1 for that !

@jashkenas
Copy link
Owner

Thanks, but no thanks, for all the reasons detailed in the original ticket. We don't want to mint a new "standard" JavaScript property that works for class-defined prototypes, but not regular function-defined ones.

@jashkenas jashkenas closed this Apr 16, 2012
@paulmillr
Copy link
Author

@jashkenas then why do we have __super__? It doesn't work for regular function-defined ones. Well, it works with additional code, but just as this one. Original ticket ended with your talk with @hornairs and merging of constructor.name patch.

P.S.
It's absolutely not needed for debugging, but it's very useful for dynamic programming.

@michaelficarra
Copy link
Collaborator

Dynamic programming? I don't think that term means what I think you think it means.

edit: Ah, I haven't heard this usage of the term before. Damn my algorithmic background!

@paulmillr
Copy link
Author

@michaelficarra yea, didn't know about this one. Won't be using it in the future in the context.

edit: ✌️

@chrismcv
Copy link

chrismcv commented May 2, 2012

coming in late on this, but the lack of a class name property is a real shame for me too.

@aseemk
Copy link
Contributor

aseemk commented May 14, 2012

I too would love to see some classname property; we too use it to auto-detect types. __name__ doesn't seem harmful to me, much like __super__.

@karellm
Copy link

karellm commented May 22, 2012

Coffeescript generated code will throw an exception (only in Safari for now) if you use use strict:

TypeError: Attempted to assign to readonly property.

The property in question is name on a code like Bla.name = 'Bla';.

Whatever the solution you want to come up with, it is a problem with coffeescript that would be nice to fix. Coffeescript should play nicely with 'use strict'.

@jashkenas
Copy link
Owner

Should be fixed in the latest version of CoffeeScript.

@paulmillr
Copy link
Author

@karellm coffeescript doesn't generate .name since 1.3.3.

@karellm
Copy link

karellm commented May 22, 2012

Thanks for the prompt answer. That must be the coffee-rails gem that hasn't updated coffeescript yet then.

Edit: Actually was up to date but I needed to edit the files to regenerate them.

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

Successfully merging this pull request may close these issues.

8 participants