|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
function CompareFunction(propertyName){
function Compare(object1,object2)
{
var value1=object1[propertyName];
var value2=object2[propertyName];
if(value1<value2){return -1;}
else if(value1>value2){return 1;}
else {return 0;}
}
return(Compare());
}
var data=[{name:"Zachary",age:"28"},{name:"Nicholas",age:29}];
data.sort(CompareFunction('name'));
alert(data[0].name);
data.sort(CompareFunction('age'));
alert(data[0].name);
这段代码错误在哪里?求解惑改成一下代码就能运行了为什么?
function CompareFunction(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;}
};
/*return(Compare());*/
}
var data=[{name:"Zachary",age:"28"},{name:"Nicholas",age:29}];
data.sort(CompareFunction('name'));
alert(data[0].name);
data.sort(CompareFunction('age'));
alert(data[0].name);
|
|