Are JavaScript strings immutable? Do I need a “string builder” in JavaScript?

后端 未结 10 2594
挽巷
挽巷 2020-11-22 02:48

Does javascript use immutable or mutable strings? Do I need a \"string builder\"?

10条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 03:24

    from the rhino book:

    In JavaScript, strings are immutable objects, which means that the characters within them may not be changed and that any operations on strings actually create new strings. Strings are assigned by reference, not by value. In general, when an object is assigned by reference, a change made to the object through one reference will be visible through all other references to the object. Because strings cannot be changed, however, you can have multiple references to a string object and not worry that the string value will change without your knowing it

提交回复
热议问题