JavaScript 字符串赋值运算符

发布于 2024-08-11 17:13:57 字数 245 浏览 4 评论 0原文

为什么我可以在字符串上使用 +=,但不能在字符串上使用 -=

例如...

var test = "Test";
var arr = "⇔"

test += arr;
alert(test);  // Shows "Test⇔"

test -= arr;
alert(test);  // Shows "NaN"

How come I can use += on a string, but I cannot use -= on it?

For example...

var test = "Test";
var arr = "⇔"

test += arr;
alert(test);  // Shows "Test⇔"

test -= arr;
alert(test);  // Shows "NaN"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

闻呓 2024-08-18 17:13:57

简短的答案是 - 它没有被定义为与字符串一起使用。

更长的答案:如果您尝试对两个字符串进行减法运算符,它会首先将它们转换为数字,然后执行算术运算。

"10" - "2" = 8

如果您尝试非数字的内容,您会收到 NaN 相关错误:

"AA" - "A" = NaN

The short answer is - it isn't defined to work with strings.

Longer answer: if you try the subtraction operator on two strings, it will first cast them to numbers and then perform the arithmetic.

"10" - "2" = 8

If you try something that is non-numeric, you get a NaN related error:

"AA" - "A" = NaN
暖心男生 2024-08-18 17:13:57

因为+运算符连接字符串,而-运算符仅将数字相减。

至于为什么——可能是因为很难确定人们在字符串相减时想要做什么。

例如:

"My string is a very string-y string" - "string"

这应该做什么?

Because the + operator concatenates strings, but the - operator only subtracts numbers from each other.

As to the why -- probably because it is difficult to determine what people want to do when they subtract strings from each other.

For example:

"My string is a very string-y string" - "string"

What should this do?

葬心 2024-08-18 17:13:57

如上所述,-= 运算符未重载以用于字符串,它仅适用于数字。

如果您尝试将其与字符串一起使用,则该运算符将尝试将两个操作数转换为 Number,这就是您得到 NaN 的原因,因为:

isNaN(+"foo"); // true

要摆脱 < test 字符串上的 code>arr 内容,您可以 替换它:

var test = "Test",
    arr = "⇔"

test += arr;
alert(test);  // Shows "Test⇔"

test = test.replace(arr, ""); // replace the content of 'arr' with "" on 'test'
alert(test);  // Shows "Test"

As all said, the -= operator is not overloaded to work with Strings, it only works with Numbers.

If you try to use it with strings, the operator will try to convert both operands to Number, that is why you are getting NaN, because:

isNaN(+"foo"); // true

To get rid of the arr content on your test string, you can replace it:

var test = "Test",
    arr = "⇔"

test += arr;
alert(test);  // Shows "Test⇔"

test = test.replace(arr, ""); // replace the content of 'arr' with "" on 'test'
alert(test);  // Shows "Test"
网名女生简单气质 2024-08-18 17:13:57

这是因为减号不是有效的字符串运算符,而加号被重载以处理数字(加法运算符)和字符串(连接运算符)。

您希望从中得到什么结果?

That's because the minus sign is not a valid String operator, whereas the plus sign is overloaded to handle both Numbers (addition operator) and Strings (concatenation operator).

What results were you hoping to get from this?

绮烟 2024-08-18 17:13:57

通常,编程语言不定义字符串减法。 += 首先并不是真正的加法,而是连接。

Generally, programming languages don't define subtraction for strings. += isn't really addition in the first place, it's concatenation.

流年已逝 2024-08-18 17:13:57

因为+(加号)也是字符串连接运算符,而-(减号)仅适用于减法。如果 JavaScript 可以将 2 个字符串附加在一起,它不会抱怨,但如果你尝试减去 2 个字符串,它就没有任何意义。

Because the +(plus sign) is also the string concatenation operator, while the -(minus sign) only applies to subtraction. If JavaScript can append 2 strings together it won't complain, but if you try to subtract 2 strings, it just doesn't make any sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文