javascript JS 字符串“+” vs concat 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16124032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS strings "+" vs concat method
提问by Artemis
I have some experience with Java and I know that strings concatenation with "+" operator produces new object.
我对 Java 有一些经验,我知道用“+”运算符连接字符串会产生新对象。
I'd like to know how to do it in JS in the best way, what is the best practice for it?
我想知道如何以最好的方式在 JS 中做到这一点,它的最佳实践是什么?
回答by laktak
MDN has the following to say about string.concat()
:
MDN 有以下几点要说string.concat()
:
It is strongly recommended to use the string concatenation operators (+, +=) instead of this method for perfomance reasons
出于性能原因,强烈建议使用字符串连接运算符 (+, +=) 而不是此方法
Also see the linkby @Bergi.
另请参阅@Bergi的链接。
回答by Ozerich
In JS, "+" concatenation works by creating a new String
object.
在 JS 中,“+”连接通过创建一个新String
对象来工作。
For example, with...
例如,与...
var s = "Hello";
...we have one object s.
...我们有一个对象s。
Next:
下一个:
s = s + " World";
Now, sis a new object.
现在,s是一个新对象。
2nd method: String.prototype.concat
回答by Xotic750
There was a time when adding strings into an array and finalising the string by using join
was the fastest/best method. These days browsers have highly optimised string routines and it is recommended that +
and +=
methods are fastest/best
曾几何时,将字符串添加到数组中并使用 using 最终确定字符串join
是最快/最好的方法。这些天浏览器具有高度优化的字符串例程,建议+
和+=
方法是最快/最好的
回答by RevanthKrishnaKumar V.
- We can't concatenate a string variable to an integer variable using
concat()
function because this function only applies to a string, not on a integer. but we can concatenate a string to a number(integer) using + operator. - As we know, functions are pretty slower than operators. functions needs to pass values to the predefined functions and need to gather the results of the functions. which is slower than doing operations using operators because operators performs operations in-line but, functions used to jump to appropriate memory locations... So, As mentioned in previous answers the other difference is obviously the speed of operation.
- 我们不能使用
concat()
函数将字符串变量连接到整数变量,因为该函数仅适用于字符串,而不适用于整数。但是我们可以使用 + 运算符将字符串连接到数字(整数)。 - 众所周知,函数比运算符慢得多。函数需要将值传递给预定义的函数并需要收集函数的结果。这比使用运算符进行操作要慢,因为运算符执行内联操作,但是函数用于跳转到适当的内存位置......所以,正如前面的答案中提到的,另一个区别显然是操作的速度。
<!DOCTYPE html>
<html>
<body>
<p>The concat() method joins two or more strings</p>
<p id="demo"></p>
<p id="demo1"></p>
<script>
var text1 = 4;
var text2 = "World!";
document.getElementById("demo").innerHTML = text1 + text2;
//Below Line can't produce result
document.getElementById("demo1").innerHTML = text1.concat(text2);
</script>
<p><strong>The Concat() method can't concatenate a string with a integer </strong></p>
</body>
</html>
回答by Aghilas Yakoub
You can try with this code (Same case)
您可以尝试使用此代码(相同情况)
chaine1 + chaine2;
I suggest you also (I prefer this) the string.concat method
我建议你也(我更喜欢这个)string.concat 方法