This post inspired me. I've made some tests.
console.log( false, 5 );
prints false 5
, and it's ok.
console.log( ( false, 5 ) );
prints 5
. Now we know that it's ok too because ( false, 5 )
returns 5
.
But why does console.log( false, {}, 5 );
print false Object {} 5
?
Also console.log( ( false, {}, 5 ) );
and even console.log( ( false, { i:0 }, 5 ) );
both prints 5
. Why is 5
is preferred to {}
?
You can see here: /
This post inspired me. I've made some tests.
console.log( false, 5 );
prints false 5
, and it's ok.
console.log( ( false, 5 ) );
prints 5
. Now we know that it's ok too because ( false, 5 )
returns 5
.
But why does console.log( false, {}, 5 );
print false Object {} 5
?
Also console.log( ( false, {}, 5 ) );
and even console.log( ( false, { i:0 }, 5 ) );
both prints 5
. Why is 5
is preferred to {}
?
You can see here: http://jsfiddle/3uUwY/
Share Improve this question edited May 23, 2017 at 11:52 CommunityBot 11 silver badge asked Sep 1, 2013 at 10:44 Ivan ChernykhIvan Chernykh 42.2k13 gold badges136 silver badges148 bronze badges4 Answers
Reset to default 6The ma operator always returns the last element, which is 5.
When using brackets you are forcing Javascript to evaluate that expression.
console.log(a, b, c); // 3 parameters, the function prints a, b and c
console.log((a, b, c)); // 1 parameter. It prints the result of
// evaluating (a, b, c) and, as it's said
// in the other answer, it returns the last element
// of the expression.
By putting brakets you make only one argument to console.log. So following
console.log( false, 5 ); // here you are using log function with 2 argumetns
And here
console.log( ( false, { i:0 }, 5 ) ); // here is only one argument.
Inside a brakets you are using ma operator.
And the ma operator always returns last expression.
So you could rewrite your expression like this:
var x = ( false, { i:0 }, 5 ); // x is 5 here
console.log( x );
By putting brakets you make only one argument to console.log. So following
console.log( false, 5 ); // here you are using log function with 2 argumetns
And here
console.log( ( false, { i:0 }, 5 ) ); // here is only one argument.
Inside a brakets you are using ma operator.
And the ma operator always returns last expression.
So you could rewrite your expression like this:
var x = ( false, { i:0 }, 5 ); // x is 5 here
console.log( x );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744214591a4563492.html
评论列表(0条)