//函數嵌套,上面兩種函數,本身好好體會
function creatCompareFunction(propertyName) { return function(object1, object2) { var value1 = object1[propertyName]; var value2 = object2[propertyName]; if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; } } } var data = [{ name: 'dd', age: 2 }, { name: 'bb', age: 31 }, { name: 'cc', age: 1 } ]; data.sort(creatCompareFunction('name')); alert(data[0].name);