What does immutable mean?

后端 未结 9 726
借酒劲吻你
借酒劲吻你 2020-11-29 16:07

If a string is immutable, does that mean that.... (let\'s assume JavaScript)

var str = \'foo\';

alert(str.substr(1)); // oo

alert(str); // foo
9条回答
  •  执笔经年
    2020-11-29 16:48

    Immutable means that which cannot be changed or modified.

    So when you assign a value to a string, this value is created from scratch as opposed to being replaced. So everytime a new value is assigned to the same string, a copy is created. So in reality, you are never changing the original value.

提交回复
热议问题