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
the ability to check if something is function or class or an arrow function #178
Comments
https://overreacted.io/how-does-react-tell-a-class-from-a-function/ allows checking this. I think this is somewhat considered a public API. I couldn't find any rationale why React users should be able to do this check. The quote is written from the perspective of React internals.
With regards to function vs arrow function: Why should this matter? You can't distinguish that with you babel setup because you probably transpile for a target that doesn't support arrow functions. |
0.Thank you for the answer @eps1lon!The link to how-does-react-tell-a-class-from-a-function is wonderful! There was no arguing for or against something. We were just seeking whether this useful or not. And if it might be helpful somehow, maybe, probably, perhaps. And article clearly:
In relation to the other questions mentioned :1.
We were just wondering if we could share some useful information for the core. And now it seems useless as its something well-known. Just because it was written here 0000-create-element-changes#deprecate-module-pattern-components
, and we thought it is some sort of thing that is hard to deal with. So, finally, there is nothing to help with, it is just the way it is ) 2.
It is true that class works like sugar. And it is a bit tricky to implement class without using
Perhaps that is why 3.
Yes, yes, the same, it is clearly explained with the information in the article provided. It is this way because it is aways transpiled to a function ) |
There is a way to fix this behaviour: for Arrow Functionsconst workingArrow = () => {};
// right after arrow function definition
Object.defineProperty(workingArrow, 'prototype', {
value: undefined
}); for Classesclass WorkingClass {}
// right after class definition
Object.defineProperty(WorkingClass, 'prototype', {
writable: false
}); Just in case it might worth opening PR to babel, what do you think? |
Tried to start PR for babel babel/babel#12115 |
If you have comments for that RFC, please leave it on the RFC. |
Hi, folks )
In a relation to document 0000-create-element-changes.md, seems it would be nice to have an ability to check if something is class or function or an arrow function, that quotation:
As far as we know there might be some sort of confusion to implement this checks. But, indeed, it is possible in runtime with no usage of
.toString()
serialization. The short example of what does it mean to implementisClass(fn)
:And the last line for isFunction would end with
true
keyword. This means there is strict difference between functions and classes, which might be tracked. The gist with full sum of checks is here: gist_link.Actualy my colleague @ZeroWheel being active React developer asked me to start this issue. And we don't know if it is useful.
The last thing I'd wish to add is about known buggy behaviour of Babel.js: there is no difference between class, function or arrow function after transpilation. So, seems it might be some sort of unpredictable behaviour. Full checks are here: babel_buggy_behaviour thanks for @denis-churbanov.
This Babel behaviour comes from ancient times, when there were no classes or arrow functions. They use corejs, and it might look not so bad, but it might be strange with configuring: I'm unable to figure out with proper config of polyfilling.
And absolutely the same is TypeScript behaviour if targeting below ES2015.
Hope this information might help somehow.
The text was updated successfully, but these errors were encountered: