var person ={name:"n"}
var people=[{name:"n"}];
var morePeople=[person];
alert(people.indexOf(person)) ; //-1
alert(morePeople.indexOf(person)); //0
//不明白people和morePeople两者之间的差别。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
归根结底,你问题是为什么
{name: "n"} === {name: "n"}结果是false。那你要去看看Comparison operators按我的理解,变量保存的是对象的引用,所以,people.indexOf(person),求的应该是它保存的引用在people中的位置,但是虽然 person 指向的对象的值虽然是{name:"n"},但是,跟people中的{name:"n"},并不是同一个对象,所以,就返回了 -1.
比people非彼people。不是一个对象
indexOf方法在ES5中仅有字符串和数组有这种方法。
而对象没有这方法。