简体   繁体   English

如果没有发生替换,javascript 字符串替换是否创建一个新字符串?

[英]Does javascript string replace create a new string if no replacement happened?

    let s = 'ABC';
    s = s.replace('D', 'E');

Is s the same string (in the memory aspect, not it's value) after this?在此之后s相同的字符串(在 memory 方面,不是它的值)吗?

According to the MDN, it says the return value is A new string, with some or all matches of a pattern replaced by a replacement.根据 MDN,它说返回值是A new string, with some or all matches of a pattern replaced by a replacement. , but I'd like to know if the special case with "NO" matches would return the original string without creating a new one. ,但我想知道“NO”匹配的特殊情况是否会返回原始字符串而不创建新字符串。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Strings are immutable in Javascript so the variable will now point to a new reference in memory with the new value.字符串在 Javascript 中是不可变的,因此该变量现在将指向 memory 中具有新值的新引用。 However the previous value will still exist in the old location in memory until it is garbage collected.然而,之前的值仍将存在于 memory 中的旧位置,直到它被垃圾回收。

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

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