-
Notifications
You must be signed in to change notification settings - Fork 2k
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
no way of getting constructor name #714
Comments
I do recall someone bringing up a scoping issue regarding this in another issue, but I can't seem to remember which one. If #640 (executable class bodies) is ever implemented, my proposal should fix this problem. |
Yeah. We had this and it was really cool, but there's a bug in IE6 with scoping when you have a named function, so it was removed. |
@michaelficarra: Cool, i'll be following #640 (+1 to executable class bodies) @Tesco: Aww. I hate IE6 even more. I really hope this gets reintroduced into coffee. |
We shouldn't seriously support IE6 any more, should we? I mean the beast is 9 years old and the last time I recall seeing someone even in a corporate environment using it was two or three years ago. Time to move on... I wonder if IE7 still has the leaky implementation of named functions. |
Sadly, yes.
|
I've had a crack at a fix for this. #729 |
This and proposals that solve this have been discussed and shot down way too many times (add #684, #675 do the list) Using the createDisplayName function on #675, tweaked to check if the variable is a prototype and its prototype is not empty, will actually name all classes. Coffescript or not. On the example, "clas.includes && clas.includes(JS.Kernel)" check is for JS.Class only. |
Here's an idea. CoffeeScript can call the Implemented here: https://github.com/sstephenson/coffee-script/compare/extended-child-name |
Great and simple tweak sstephenson. I'd normally argue that explicitely requiring to sublcass a base class is a problem, however, this is not so on my project (most classes already have to extend a base class). |
I've merged in a variant of Tesco's fix for class names, and you can now access the "name" property of the constructor function, at the cost of having slightly more verbose compiled output... closing the ticket. |
Great! Will start using it asap. |
Sorry folks, I had to revert this commit because (surprise, surprise) it didn't work in Internet Explorer. Things are back as before on master at the moment. |
And one more update. Tesco was quite right about the pure-function-declaration-ness being critical for IE to work. Here's a patch that puts it back that way, and works in internet explorer: |
I've been working around by using the plugin mechanism, and changing the source code. On 0.9.5, function.name should work ok. With one caveat. function.name is non-standard, and will not work on IE. Guess I'll still stick with metaprogramming to make coffeescript do things they way I wish it did. |
@danielribeiro: Coco amends that problem by explicitly setting |
This can be achieved with a base class if required, it uses a RegExp which isn't ideal but should work in IE. class Base
@getName: ->
this.toString().match(/^function\s(.+)\(/)[1]
class Foo extends Base
alert Base.getName() # alerts 'Base'
alert Foo.getName() # alerts 'Foo' |
(For future Googlers...) Expanding on soniciqs answer above, here's how I determine the class name in IE without base classing:
This can be single-lined (if thats the way you roll) to:
|
I believe it's currently not possible to obtain a class' constructor name.
I'd like to do the following:
Right now
@constructor.toString()
will not yield the name because the constructor is an anonymous function assigned to a variable - and therefore doesn't have a name.Would it be feasible to name the constructor function, in addition to the assignment, like so?
The text was updated successfully, but these errors were encountered: