Add named classes that work in `use strict`. #2262

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
8 participants

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 commented Apr 14, 2012

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

Collaborator

vendethiel commented Apr 14, 2012

+1 for that !

Owner

jashkenas commented Apr 16, 2012

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 closed this Apr 16, 2012

@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.

Collaborator

michaelficarra commented Apr 16, 2012

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!

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

edit: ✌️

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.

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 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'.

Owner

jashkenas commented May 22, 2012

Should be fixed in the latest version of CoffeeScript.

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

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