How can I differentiate between these things in ES6?
- arrow functions are functions that cannot be used as constructors, and don’t have a
.prototypeproperty. However, methods don’t either. They inherit from
- classes are functions that can’t be called without
new, and that have a
.prototypeobject which is normally not empty. If the
extendskeyword was used, they don’t inherit from
- functions are functions that can be called either way, and do have a
.prototypethat is normally empty. They inherit from
- generator functions are functions that do have a
.prototypewhich inherits from the intrinsic GeneratorPrototype object, and they inherit from the intrinsic Generator object.
As you can see, there are some clues. However, the properties and inheritance can always be messed with, so you cannot really trust it. Whether a function is a constructor (can be called with
new) cannot be determined from outside, you have to call it and see whether it throws – which could be faked as well.
So your best bet might be
Function.prototype.toString, to see how the source looked like. If your ES implementation supports that.
And how can I differentiate between these things in transpilers?
I don’t think any transpiler implements prototype-less arrows and methods. Whether a class constructor throws upon being called depends on the looseness of the transpilation, but that’s not a good way for distinction anyway.
toString doesn’t work either afaik.