![](/img/trans.png)
[英]Why ArraySpeciesCreate is called in Array.prototype.splice?
[英]Why can't I `.call` Array.prototype.splice on a string?
我明白我可以並且可能應該使用substring
:)
出於教育原因,我想知道為什么我不能使用call
對字符串進行拼接操作,我理解這是一個類似數組的對象。 看起來這應該工作:
Array.prototype.splice.call('filename.jpg', -3, 3).join(''); // return the last three chars
引發錯誤TypeError: Object.isSealed called on non-object
在V8中的TypeError: Object.isSealed called on non-object
。
在Javascript中,字符串是不可變的; 它們在創建后無法更改。 所以沒有“set char”或“splice”方法,因為無法更改字符串。 但是,您可以在它們上調用split('')
將它們轉換為數組,因此您可以使用'filename.jpg'.split('').splice(-3, 3).join('')
同樣的效果。
您需要先將其轉換為數組對象
Array.prototype.splice.call('filename.jpg'.split(''), -3, 3).join('');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.