<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Document</title></head><body> <script>({toString:b} = 123);console.log(b === Number.prototype.toString); // trueconsole.log(Number.prototype.toString()); // 0console.log(b()); // Number.prototype.toString is not genericlet num = 456;console.log(num.b()); // num.b is not a function </script></body></html>为什么b不能作为函数调用?
1 回答
守着一只汪
TA贡献1872条经验 获得超3个赞
The toString function is not generic; it throws a TypeError exception if its this value is not a Number or a Number object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
翻译一下后面的:
如果他的
this
值不是数字类型或者Number
对象,将会抛出一TypeError
直接调用this
是window
你可以这么用:
b.call(1) b.call(Number('test'))
添加回答
举报
0/150
提交
取消