Javascript 字符串是否有反向连接运算符?

Is there a reverse concatenation operator for Javascript strings?

在Javascript,

hello += ' world'
// is shorthand for
hello = hello + ' world'

是否有相反方向的shorthand运算符?

hello = ' world' + hello

我尝试了 hello =+ ' world' 但它没有用:它只是将 ' world' 类型转换为 NaN 然后将其分配给 hello.

您所描述的内容实际上并不 shorthand。

另一种方法是使用字符串的 concat 函数:

var hello = 'hello';
var reverse = 'world '.concat(hello);

Javascript 没有 'reverse' 字符串运算符,但是 Array.reverse() 函数可以在这种情况下帮助您:

var hello = "hello";

hello = (hello + ",world, beautiful").split(",").reverse().join(' ');

console.log(hello);  // beautiful world hello

Is there a shorthand operator for the opposite direction?

不,所有JavaScript compound assignment运算符都将目标作为左操作数。

只需使用您拥有的 hello = ' world' + hello; 语句即可。如果您重复执行此操作,请考虑使用数组作为缓冲区,您可以在其前面添加 unshift method.

在不修改现有数组的情况下连接 returns 数组,所以在您拥有的地方

a= a.concat(b) // 将 b 放在 a

的末尾

你可以很容易地做 a = b.concat(a)? // 将 b 放在 a 之后。 IE b 开头的列表 a