hld*_*dev 2 javascript firefox google-chrome
Function.name property is configurable and can be overriden, but if I try
function foo() {}
Object.defineProperty(foo, 'name', {value: 'bar'})
Run Code Online (Sandbox Code Playgroud)
foo.toString() still shows up as "function foo() {}" but I expected it to be "function bar() {}".
您无法 100% 可靠地做到这一点。这是因为 的定义Function.prototype.toString,它使用 [[SourceText]] 内部槽,该槽是在从用于创建函数的源代码创建函数时设置的。它不使用该name属性。
您也可以尝试覆盖该函数,但是如果有人明确使用它,toString那当然仍然会失败。Function.prototype.toString