const obj = {
get foo() {},
set foo(x) {}
};
obj.foo.name
// TypeError: Cannot read property ‘name‘ of undefined
const descriptor = Object.getOwnPropertyDescriptor(obj, ‘foo‘);
descriptor.get.name // "get foo"
descriptor.set.name // "set foo"
有两种特殊情况:bind
方法创造的函数,name
属性返回bound
加上原函数的名字;Function
构造函数创造的函数,name
属性返回anonymous
。
(new Function()).name // "anonymous"
var doSomething = function() {
// ...
};
doSomething.bind().name // "bound doSomething"