问题
I'm really confused by this. If I do something like this:[1].slice(1)
it returns an empty array (in the chrome interactive console). But if I compare:[1].slice(1) === []
it's always false. So my Question is, what does [1].slice(1) really return?
回答1:
===
compares objects by references.
You're comparing two different array objects which are both empty.
If you want to check whether an array is empty, check whether .length === 0
.
回答2:
That's not a problem of slice
or ===
.
If you do [1]==[1]
, it returns false
.
That's because both ==
and ===
compare objects by reference
回答3:
[] === []
also returns false. [1].slice(1)
does in fact return []
回答4:
You better check the length:
[1].slice(1).length; // falsey
来源:https://stackoverflow.com/questions/11938712/array-slice-on-array-with-one-element