简体   繁体   English

在Javascript中,所有内置对象都是不可变的吗?

[英]In Javascript are all built-in objects immutable?

I know that String and Number objects are immutable but are all built-in objects immutable as well? 我知道String和Number对象是不可变的,但所有内置对象都是不可变的吗? If it's not true, can you give me an example of a built-in object that is mutable? 如果不是这样,你能给我一个可变的内置对象的例子吗?

Arrays are mutable. 数组是可变的。 Here are a list of mutable methods for arrays. 以下是数组的可变方法列表。

push - Elements are added on to the end of the array push - 将元素添加到数组的末尾

pop - Elements are removed from the end of the array pop - 从数组末尾删除元素

shift - Elements are removed from the beginning of the array shift - 从数组的开头删除元素

unshift - Elements are added to the beginning of the array unshift - 将元素添加到数组的开头

splice - Adds/removes elements to an array splice - 向数组添加/删除元素

None of these methods create a new array, but modify the existing array. 这些方法都不会创建新数组,而是修改现有数组。

Nope, for instance an array is mutable. 不,例如,数组是可变的。 If you have an array of strings and you push a string to it, you don't get a new array with an extra string added to it. 如果你有一个字符串数组并且你将一个字符串推送到它,你就不会得到一个新数组,并添加了一个额外的字符串。 The original array is altered. 原始数组被更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM