I am designing an API and I want to find a way, to figure out if a function expression is a arrow function.
var o1 = {bar:function(){
return this.value;
}};
var o2 = {bar:() => {
return this.value;
}}
function foo(o){
let baz = {value:10};
return o.bar.call(baz)
}
console.log(foo(o1)) //10
console.log(foo(o2)) // undefined
Now before I get downvoted because of the obvious answer that es6 arrow functions have a lexically bound this
.
Thats not what I am asking here. I want a way to programmatically find out if an arrow function is being used, so I can throw. Is it possible?
It seems like a easy way to go about this would be to check the name
property
function foo(f){
if(!f.name){
throw Error("f is an arrow function")
}
let baz = {value:10};
return f.call(baz)
}
console.log( foo(() => this.value) ) // f is a arrow function
But then it looses its object form. As soon as an es6 function expression becomes a property on an object, it has a name value, so I cant test then. What else can one test for?