Erm, I tried my best to explain what my problem is, sorry for by bad English.
In the below code:
function createCompareFunction(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 person = [{name: "Nicholas", age: 29}, {name: "Alex", age: 34}];
person.sort(createCompareFunction("name"));
alert(person[0].name);
as you can see in this scope:
return function(object1, object2){
var value1 = object1[propertyName];
var value2 = object2[propertyName];
there’s object1 and object2 arguments
but, when i call the function, i didnt defined the arguments, but why it automatically knows that it is the object in the array.
Hope you know what i’m talking. Thanks!!
See the specification for sort. It tells you what arguments it passes when it calls the function you pass to it (which are the two items in the array that it is currently sorting).