function comParison(propertyName){
return function(obj1,obj2){
var value1 = obj1[propertyName];
var value2 = obj2[propertyName];
if (value1 < value2){
return -1;
}else id (value1 > value2){
return1;
}else{
return 0;
}
}
};
var data = [{name:n1},{name:n2}];
data.sort(comParison("name"));
这将比较名称,但是 comParison 函数如何访问名称属性以及为什么它必须作为字符串传递?