admin管理员组文章数量:1289516
Inspired by this video, I tested further with {}+[]
.
Test 1:
typeof {}+[] //"object"
Okay, so {}+[]
is an object
.
Test 2:
var crazy = {}+[];
typeof crazy //"string"
What? Didn't {}+[]
is an object
? Why is it a string
now?
Test 3:
console.log({}+[])
What I got:
So it is a number
!... No?
So what actually is the type of {}+[]
??
UPDATED
To people who say {}+[]
is a empty string:
{}+[] === "" //false
({}+[]) === "" //false
({};+[]) === "" //SyntaxError
({}+[]).length //15
JavaScript is so hard to understand...
Inspired by this video, I tested further with {}+[]
.
Test 1:
typeof {}+[] //"object"
Okay, so {}+[]
is an object
.
Test 2:
var crazy = {}+[];
typeof crazy //"string"
What? Didn't {}+[]
is an object
? Why is it a string
now?
Test 3:
console.log({}+[])
What I got:
So it is a number
!... No?
So what actually is the type of {}+[]
??
UPDATED
To people who say {}+[]
is a empty string:
{}+[] === "" //false
({}+[]) === "" //false
({};+[]) === "" //SyntaxError
({}+[]).length //15
JavaScript is so hard to understand...
Share Improve this question edited Apr 29, 2012 at 22:27 Derek 朕會功夫 asked Apr 29, 2012 at 22:15 Derek 朕會功夫Derek 朕會功夫 94.4k45 gold badges197 silver badges253 bronze badges 9-
I think in test1, typeof finds first paramater's type -> {}, and it's an object, try
typeof ({}+[])
this one, it's string. – Okan Kocyigit Commented Apr 29, 2012 at 22:20 -
@pcamal, So is it a number or a string? (Thought
{}+[]
would beobject
, because both of them are.) – Derek 朕會功夫 Commented Apr 29, 2012 at 22:22 -
1
Regarding your update:
{}+[] === ""
is evaluated as{}; +[] === "";
, i.e. empty block and+[] === ""
.{}+[] === 0
yieldstrue
. – Felix Kling Commented Apr 29, 2012 at 22:29 -
JavaScript is so hard to understand: Well, that's because you are ambiguous ;) When encountering
{}
in{}+[] === ""
the parser does not know whether{}
should indicate an object literal or a block. Since this is not in an expression context,{}
is interpreted as block (the default behavior). The parenthesis(...)
force an evaluation as expression. – Felix Kling Commented Apr 29, 2012 at 22:34 -
2
I just added this to indicate that the line
{}+[]
is interpreted as two statements and not as one. But since JavaScript has automatic semicolon insertion it might actually do this. Why? Because if some syntax is ambiguous, a decision has to be made how to interpret it. In this case, the developers decided to interpret{}+[]
asblock
,unary plus
,array
and not asobject
,addition operator
,array
. You might not agree with this, but that's how it is :) – Felix Kling Commented Apr 29, 2012 at 22:50
2 Answers
Reset to default 13Type of {}+[]
may vary depending on the context.
typeof {}+[] //"object"
As per operators precedence in this casetypeof {}
evaluates to "object",+[]
adds an empty string(array is coerced to string) therefore result is "object".
You could think of checkingtypeof ({}+[])
(your second case).var crazy = {}+[]; typeof crazy //"string"
In this case you are adding object and array - they both coerce to string, thereforetypeof
returns "string".{}+[]
This is interpreted as an empty block of code, unary plus and empty array. First part does nothing, array is converted to a ma-separated string of it's elements(empty string for empty array), then to a number(empty string is converted to 0), hence0
.
UPDATED
{}+[] === "" //false
see #3,{}
is interpreted as a block, you are getting0
on the left.
Compare{}+[] === 0 // true
.({}+[]) === "" //false
see #1,{}
is interpreted as an object literal. When trying to add array and object, they both convert to string,"[object Object]"
for object and empty string for array. Hence, you are getting"[object Object]"
on the left.
Compare({}+[]) === "[object Object]" // true
.({};+[]) === "" //SyntaxError
I guess, this one is self-explanatory :)({}+[]).length //15
15 is exactly the length of"[object Object]"
, see above.
var f = {}+[];
console.log(f==="[object Object]")
this will log true
because when you assign {}+[]
to the var it converts it to string by using the toString()
method of it... which in this case returns "[object Object]" (which is 15 letters long - and that where the length=15 is from)
本文标签: javascriptProblems with adding object with arrayStack Overflow
版权声明:本文标题:javascript - Problems with adding object with array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741437921a2378744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论