node.js - An incomprehensible part of JavaScript
ringa_lee
ringa_lee 2017-05-16 13:34:41
0
4
498

I am learning JavaScript (NodeJs) recently
I found a problem when using for to traverse a JsonArray
I wrote it like this:

Logically speaking, it should be wrong to write like this. You should judge whether it is Null, but I ran it and it succeeded. There was no error and the output was normal.
Black question mark face...
Please tell me this What is the principle...
Could it be that obj2=obj1.result[i] will return a boolean? ?
I have learned JAVA before, but I feel that JavaScript is a bit less rigorous than JAVA, and my thoughts are a bit restricted...

ringa_lee
ringa_lee

ringa_lee

reply all(4)
我想大声告诉你

Assignment expressions return the assigned value. That is to say obj2=obj1.result[i]会返回obj1.result[i].

js is a dynamic language. It has the feature of privacy conversion, that is, when a certain data type is needed, it will convert the incoming value into a specific type through pre-set rules.

Then just perform type conversion on the assignment expressionobj2=obj1.result[i]中返回值是obj1.result[i],那js引擎就会对obj1.result[i] into Boolean type.

If you obj1.result[i]的值是除NaN,0,undefined,null,''之外的其他值,都会转换成true,否则则是false.

phpcn_u1582

The return result of the assignment operator is the assigned value. This is defined in all major C languages, including Java. It is not surprising.

Conditional judgment in Java can only use Boolean values, but JavaScript is different. A Boolean value is required here. If the value you give is not a Boolean value, it will be implicitly converted to a Boolean value.

为情所困

Javascript is a weakly typed language and will be converted automatically~

我想大声告诉你

An equal sign means assignment, which means you assigned obj1.result[i] to obj2, so it is always true

You can search for the difference between = == === and you will understand after reading the question

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template