<!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>
Pourquoi b ne peut-il pas être appelé en tant que fonction ?
Numéro.prototype.toString standard
Traduisez ce qui suit :
Appelez directement
this
是window
Vous pouvez utiliser ceci :
Vous pouvez
b.call(num)
,一般来说toString
不允许作为普通函数执行很容易接受,就跟构造函数一般不作为普通函数执行一样。ps:例子中的
Number.prototype.toString()
实际上作用域也是Number.prototype
Pour ajouter, la réponse est un peu hors sujet. b() est en fait appelé en tant que fonction, et l'appel est réussi. L'erreur est renvoyée par toString() lui-même.
Number.prototype.toString
Peut être appelée en tant que fonction mais celle-ci doit être de type Number. La même chose s'applique aux autres types de toString.15.7.4.2 Number.prototype.toString