I want to get a function's name in an arrow function
Actually, if we write
function abc(){
let funcName = arguments.callee.name
}
But in an arrow function
abc: ()=>{
let funcName = arguments.callee.name
}
funcName return ''
arguments binding. But they have access to the arguments object of the closest non-arrow parent function. Named and rest parameters are heavily relied upon to capture the arguments passed to arrow functions.function dummy() {
return (() => arguments.callee.name)(); // dummy
}
this inside an arrow function remains the same throughout the lifecycle of the function and is bound to the value of the closest this-providing environment.const obj = {
// like a normal function, returns obj - defines like obj.a = function a() {};
a() { return this },
// arrow function uses global as this
b: () => { return this },
};
console.log(obj.a() == obj) // true
console.log(obj.b() == obj) // false
console.log(obj.b() == window) // true
Therefor, the only way to get the name of an arrow function is from accessing the direct reference to it.
const arrow = () => arrow.name;
console.log(arrow()); // "arrow"
const obj = {
a: () => obj.a.name;
}
console.log(obj.a()); // "a"
arguments)", I don't see how this answers the question about getting the .namethis in the closest non-arrow parent function." Probably good enough for most use cases but just for completeness: It's the closest "this-providing" environment, which can be the global environment or a module environment (not that those this values would be very interesting, but still).
12argumentsobject inside the arrow function?abcproperty of a parent object. You could just hard code it in the arrow function if you want.