【发布时间】:2014-06-23 12:17:56
【问题描述】:
是否可以让数组的元素告诉它在 JavaScript 中的位置?如果可能的话,当您有一个对象数组并且需要调用对象字符串中的一个方法时,它可能会派上用场。您可以将它的索引作为方法参数传递,但这看起来很麻烦,而且可能没有必要。
理想情况下,应该是这样的:
function ArrayType(){
this.showIdentity = function(){
alert(this.indexOf()); // This obviously doesn't work, but I'm looking for
// a method that will return the index of "this"
// in its parent array.
}
}
var myArray = new Array();
myArray[0] = new ArrayType;
myArray[1] = new ArrayType;
myArray[1].showIdentity(); // Should alert "1"
有人知道这个问题的解决方案吗(除了将索引与方法一起传递)?
【问题讨论】:
标签: javascript arrays object methods indexing