<!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>
Mengapa b tidak boleh dipanggil sebagai fungsi?
Number.prototype.toString standard
Terjemah yang berikut:
Hubungi terus
this
是window
Anda boleh menggunakan ini:
Anda boleh
b.call(num)
,一般来说toString
不允许作为普通函数执行很容易接受,就跟构造函数一般不作为普通函数执行一样。ps:例子中的
Number.prototype.toString()
实际上作用域也是Number.prototype
Untuk menambah, jawapannya agak keluar dari topik b() sebenarnya dipanggil sebagai fungsi, dan panggilan itu berjaya dilemparkan oleh toString() itu sendiri.
Number.prototype.toString
Boleh dipanggil sebagai fungsi tetapi ini mestilah jenis Nombor. Perkara yang sama berlaku untuk jenis toString yang lain.15.7.4.2 Number.prototype.toString