[removed] sort objects

后端 未结 4 716
温柔的废话
温柔的废话 2021-01-13 22:46
function Player() {
  var score;

  this.getScore = function() { return score; }
  this.setScore = function(sc) { score = sc; }
}

function compare(playerA, playerB)         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 23:01

    It's probably because you don't have any "array values" inside your array - textual indexes are not regarded as array values but as object propertiest (arrays are "objects in disguise" in javascript). You can add as many properties to any object but array specific methods like sort take only "real" array members as their parameteres (i.e. only with numerical indexes)

    var arr = new Array()
    arr[0] = 1
    arr[1] = 2
    arr["textual_index"] = 3
    alert(arr.length);
    

    The last line alerts "2" not "3" since there are only two values with numeric indexes.

提交回复
热议问题