Javascript 类型(Int/String)问题
我在 JavaScript 中遇到了一些奇怪的事情(可能不是,更有可能的是我没有真正理解它),我很想知道为什么事情会这样。
当我这样做时:
var index = '1';
index++;
alert(index);
index = index + 1;
alert(index);
index = true ? index + 1 : 0;
alert(index);
如 http://jsfiddle.net/5mdmJ/ 所示,警报将变为“2”, “3”,“4”
当我颠倒顺序并执行此操作时(http://jsfiddle.net/5mdmJ/1/):
var index = '1';
index = true ? index + 1 : 0;
alert(index);
index = index + 1;
alert(index);
index++;
alert(index);
我将得到“11”,“111”和“112”。
我确实知道索引是字符串,但我真的不明白为什么它在示例 1 中一直是 int 类型,而在示例 2 中是字符串类型。 我知道这可能非常简单,但到目前为止我找不到任何东西可以真正向我阐明正在发生的事情背后的逻辑。类型有变化吗?为什么以及何时会发生这种情况?
感谢您的任何提示或文章或其他什么!
I have encountered something weird (probably not, it's more likely that I don't really get it) in JavaScript and I'd be curious to find out why things behave like they do.
When I do:
var index = '1';
index++;
alert(index);
index = index + 1;
alert(index);
index = true ? index + 1 : 0;
alert(index);
as in http://jsfiddle.net/5mdmJ/ the alerts will go "2", "3", "4"
When I reverse the order and do this (http://jsfiddle.net/5mdmJ/1/):
var index = '1';
index = true ? index + 1 : 0;
alert(index);
index = index + 1;
alert(index);
index++;
alert(index);
I'll have "11", "111" and "112".
I do know that this is something with index being a string, but I don't really get why it's int-typed all the way through in example 1 and string-typed in exampled two.
I know this is probably going to be really simple but I could not find anything by now that really clarifies to me the logic behind what's going on. Does the type change? Why and when does this happen?
Thanks for any hint or article or whatever!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于字符串和整数,重载了单个加号运算符。在第一个示例中,仅为
int
定义了++
运算符,因此索引被转换为数字,然后增加。之后,加号运算符表示加法。由于第二个示例中的索引是字符串,因此加号运算符表示连接。The single plus operator is overloaded for strings and ints.In the first example, the
++
operator is only defined forint
s, so index gets converted to a number, then incremented. Afterwards, the plus operator indicates addition. Since index is a string in the second example, the plus operator indicates concatenation.与
+
不同,它有两种含义(数字加法,字符串连接),++ 没有歧义 - 它始终意味着“增量”,
因此当您在字符串上运行
++
时,它会将其转换为数字。由于示例 #2 中没有发生这种情况,因此+
操作都是串联。Unlike
+
, which has two meanings (addition for number, concatenation for strings),++
has no ambiguity - it always means "increment"So when you run
++
on a string, it converts it into a number. Since this doesn't happen in example #2, the+
operations are all concatenations.答案是,由于 js 是松散类型的,它从您执行的第一个操作开始。
在示例 1 中,第一个操作是独占算术运算,js 正确解释它并始终将其视为 INT
在示例 2 中,第一个操作是比较操作,js 将其解释为布尔值,然后将其立即关闭属性字符串。
这就是为什么你会有不同的行为。
The answer is that since js is loosely typed it starts with the first operation that youre performing.
In your example 1 the first operation is an exclusive arithmetic operation and js correctly interprets it and considers it INT all the way
In your example 2 the first operation is an comparsion operation and js interprets it as boolean and then its immediately close property string.
Thats why you get different behaviours.
index++;
是一个数字函数。请注意,我没有说整数。 JavaScript 中不存在整数这样的东西。所有数字都是浮点数。这:
index = true ? index + 1 : 0;
是字符串连接,因为index是一个字符串。如果索引是一个数字,那么它将两者相加。所以发生的情况是 ++ 运算符将字符串转换为数字并将值相加。在第二个例子中,它将数字转换为字符串并将两个字符串附加在一起以形成一个新字符串(记住在 JavaScript 中字符串是不可变的)。
This:
index++;
is an number function. Notice that I didn't say integer. There is no such thing as an integer in JavaScript. All numbers are floating point numbers.This:
index = true ? index + 1 : 0;
is string concatenation, because index is a string. If index is a number then it would add the two together.So what is happening is that the ++ operator is converting the string to a number and adding the values. In the second its converting the number to a string and appending the two strings together to form a new string (remember in JavaScript strings are immutable).
原因是variable++会先将变量转换为数字,然后将其加一。而变量+1只会给变量加1,但不会转换它。
The reason is that variable++ will convert the variable first to number and then increase it by one. Whilst variable + 1 will only add 1 to the variable, but not converting it.
这是因为连接字符串或变量时的类型优先级。
It's because of the type priority when you concatenate strings or variables.
我的猜测是,当您执行
index++
时,它被视为“int”,并且在示例一中保持这种状态,但是当您执行index + 1
时,它被视为“int”字符串,并且在示例 2 中保持不变..My GUESS is, that when you do
index++
, it's considered an "int", and stays that way through example one, but when you doindex + 1
, it's considered a string, and stays like that through example 2..