javascript - es6數(shù)值解構(gòu)Number.prototype.toString is not generic
問(wèn)題描述
<!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)用?
問(wèn)題解答
回答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對(duì)象,將會(huì)拋出一TypeError
直接調(diào)用this是window你可以這么用:
b.call(1)b.call(Number(’test’))回答2:
你可以b.call(num),一般來(lái)說(shuō)toString不允許作為普通函數(shù)執(zhí)行很容易接受,就跟構(gòu)造函數(shù)一般不作為普通函數(shù)執(zhí)行一樣。ps:例子中的Number.prototype.toString()實(shí)際上作用域也是Number.prototype
補(bǔ)充一下,答題有點(diǎn)離題了,b()實(shí)際上是作為函數(shù)調(diào)用的,也調(diào)用成功了,錯(cuò)誤是toString()自身拋出來(lái)的。
回答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. angular.js - webpack build后的angularjs路由跳轉(zhuǎn)問(wèn)題2. java - Activity中的成員變量被賦值之后,Activity被回收的時(shí)候內(nèi)存才會(huì)被釋放嗎3. java - web項(xiàng)目中,用戶登陸信息存儲(chǔ)在session中好 還是cookie中好,取決于什么?4. php由5.3升級(jí)到5.6后,登錄網(wǎng)站,返回的是php代碼,不是登錄界面,各位大神有知道的嗎?5. 數(shù)組按鍵值封裝!6. 我寫的哪里有錯(cuò)?請(qǐng)大神幫忙查看一下。7. tp5.0,param獲取全部參數(shù)8. 使用list和each配合,的作業(yè),輸出一行后,如何換行9. 老師,flex-shrink: 1; 按視頻操作,不會(huì)自動(dòng)縮放10. 為什么 必須在<ul> 下建立 <li> 在建<a>?
