<!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>
Why can't b be called as a function?
Number.prototype.toString standard
Translate the following:
will be thrownDirectly call
this
iswindow
You can use it like this:
You can
b.call(num)
, and generally speaking, it is easy to accept thattoString
is not allowed to be executed as an ordinary function, just like constructors are generally not executed as ordinary functions.ps: The
Number.prototype.toString()
in the example actually has the same scope asNumber.prototype
To add, the answer is a bit off topic. b() is actually called as a function and the call is successful. The error is thrown by toString() itself.
Number.prototype.toString
can be called as a function but this must be of type Number. The same applies to other types of toString.15.7.4.2 Number.prototype.toString