<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
({toString:b} = 123);
console.log(b === Number.prototype.toString); // true
console.log(Number.prototype.toString()); // 0
console.log(b()); // Number.prototype.toString is not generic
let num = 456;
console.log(num.b()); // num.b is not a function
</script>
</body>
</html>
Warum kann b nicht als Funktion aufgerufen werden?
Number.prototype.toString 标准
翻译一下后面的:
直接调用
this
是window
你可以这么用:
你可以
b.call(num)
,一般来说toString
不允许作为普通函数执行很容易接受,就跟构造函数一般不作为普通函数执行一样。ps:例子中的
Number.prototype.toString()
实际上作用域也是Number.prototype
补充一下,答题有点离题了,b()实际上是作为函数调用的,也调用成功了,错误是toString()自身抛出来的。
Number.prototype.toString
可以作为函数调用但 this 一定要是 Number 类型。其他类型的 toString 同理。15.7.4.2 Number.prototype.toString