匿名函数中的参数在命名函数中表示的内容

what the argument in the anonymous function represent in a named function?

本文关键字:函数 表示 参数      更新时间:2023-09-26

呃,我尽力解释我的问题是什么,很抱歉英语不好。在以下代码中:

    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);

正如你在这个范围内看到的:

 return function(object1, object2){
   var value1 = object1[propertyName];
   var value2 = object2[propertyName];

有object1和object2参数但是,当我调用该函数时,我没有定义参数,但为什么它会自动知道它是数组中的对象。

希望你知道我在说什么。谢谢

请参阅排序规范。它在调用传递给它的函数时告诉它传递了哪些参数(这是数组中当前正在排序的两个项)。