There is no obvious difference between an arrow function and a regular function.
({}).toString.call(function () {})
"[object Function]"
({}).toString.call(() => {})
"[object Function]"
or
console.dir( (function () {}) )
function anonymous()
arguments: null
caller: null
length: 0
name: ""
prototype: Object
__proto__: ()
<function scope>
console.dir( (() => {}) )
function anonymous()
arguments: (...)
caller: (...)
length: 0
name: ""
__proto__: ()
<function scope>
The behaviour of the two is different though and there is a valid use case for being able to tell the two apart.
How to programmatically distinguish an arrow function from a regular function?
There are differences between arrow functions and traditional functions, as well as some limitations: Arrow functions don't have their own bindings to this , arguments or super , and should not be used as methods. Arrow functions don't have access to the new.target keyword.
Main benefit: No binding of 'this' In classic function expressions, the this keyword is bound to different values based on the context in which it is called. With arrow functions however, this is lexically bound. It means that it uses this from the code that contains the arrow function.
Since regular functions are constructible, they can be called using the new keyword. However, the arrow functions are only callable and not constructible, i.e arrow functions can never be used as constructor functions. Hence, they can never be invoked with the new keyword.
let add = (x, y) => { return x + y }; JavaScript arrow functions are roughly the equivalent of lambda functions in python or blocks in Ruby, but they have more intricate details. Arrow functions allow a developer to accomplish the same result with fewer lines of code and approximately half the typing.
The best I can think of is using toString
:
let isArrowFunction;
isArrowFunction = (fn) => {
console.log(fn.toString());
return fn.toString().indexOf('function') !== 0;
};
console.log(isArrowFunction(() => {}) === true);
console.log(isArrowFunction((foo: string) => {}) === true);
console.log(isArrowFunction(function () {}) === false);
See:
(function () {}).toString();
"function () {}"
(() => {}).toString();
"() => {}"
Uhm, the requirements are a bit weird, but I made some tests and:
typeof (() => {}).prototype === "undefined"
Is true
, while:
typeof (function () {}).prototype === "undefined"
Is false
, so:
function isArrow(x)
{
return typeof (x.prototype) === "undefined"
}
Fiddle here: https://jsfiddle.net/87kn67ov/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With