array indexOf with objects?

前端 未结 3 1580
情书的邮戳
情书的邮戳 2020-12-21 18:10

I know we can match array values with indexOf in JavaScript. If it matches it wont return -1.

var test = [
    1, 2, 3
]

// Returns 2
test.indexOf(3);
         


        
3条回答
  •  误落风尘
    2020-12-21 18:46

    Nope, you can't and the explanation is simple. Despite you use the same object literal, two different objects are created. So test would have another reference for the mentioned object if you compare it with the reference you are looking for in indexOf.

提交回复
热议问题