获得函数名称为pretty straightforward:
const func1 = function() {}
const object = {
func2: function() {}
}
console.log(func1.name);
// expected output: "func1"
console.log(object.func2.name);
// expected output: "func2"
但是,如何获得一个getter / setter的字符串名称?
class Example {
get hello() {
return 'world';
}
}
const obj = new Example();
该语法设置了get
属性描述符的hello
函数,因此该函数的名称始终为get
,您可以通过[[ C0]。
hello
这样的声音无法解决您的最终问题,但:get
100%回答了以下问题:“如何在JavaScript / TypeScript中获取吸气剂/设定剂名称?”并且将始终为“ get”]