es6数值解构Number.prototype.toString is not generic

<!DOCTYPE html>

<html>

<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>

为什么b不能作为函数调用?

茅侃侃
浏览 408回答 1
1回答

守着一只汪

Number.prototype.toString 标准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'))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript