javascript - es6數(shù)值解構(gòu)Number.prototype.toString is not generic
問題描述
<!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); // 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不能作為函數(shù)調(diào)用?
問題解答
回答1:Number.prototype.toString 標(biāo)準(zhǔn)
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值不是數(shù)字類型或者Number對象,將會拋出一TypeError
直接調(diào)用this是window你可以這么用:
b.call(1)b.call(Number(’test’))回答2:
你可以b.call(num),一般來說toString不允許作為普通函數(shù)執(zhí)行很容易接受,就跟構(gòu)造函數(shù)一般不作為普通函數(shù)執(zhí)行一樣。ps:例子中的Number.prototype.toString()實際上作用域也是Number.prototype
補充一下,答題有點離題了,b()實際上是作為函數(shù)調(diào)用的,也調(diào)用成功了,錯誤是toString()自身拋出來的。
回答3:Number.prototype.toString 可以作為函數(shù)調(diào)用但 this 一定要是 Number 類型。其他類型的 toString 同理。
b.call(123)// '123'
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.
15.7.4.2 Number.prototype.toString
相關(guān)文章:
1. java固定鍵值轉(zhuǎn)換,使用枚舉實現(xiàn)字典?2. javascript - 有沒有類似高鐵管家的時間選擇插件3. 這是什么情況???4. html - 如何使用用戶輸入的數(shù)據(jù)去運行一個數(shù)學(xué)公式,最后怎么返回。5. java - HTTPS雙向認(rèn)證基礎(chǔ)上有無必要再進行加簽驗簽?6. python - flask學(xué)習(xí),user_syy添加報role is invalid keyword for User.7. 如何解決tp6在zend中無代碼提示8. css3 - less或者scss 顏色計算的知識應(yīng)該怎么學(xué)?或者在哪里學(xué)?9. vim - win10無法打開markdown編輯器10. javascript - 移動端開發(fā) H5 頁面在 iOS手機上無法實現(xiàn) 長按復(fù)制文本 求解決
