为什么如果我将对象传递给函数,我不需要在 JavaScript 中声明函数参数

发布于 2024-11-01 09:26:28 字数 779 浏览 1 评论 0原文

下面是代码:

 function createComparisonFunction(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: "Zachary", age: 28}, {name: "Nicholas", age: 29}];

    data.sort(createComparisonFunction("name")); 
    alert(data[0].name);

    data.sort(createComparisonFunction("age"));
    alert(data[0].name);

正如您所看到的,createCompatisonFunction 填充了名为 name 的值,并且您可以在该函数内部看到,有 object1 和 object2。我能问一下这些参数传递了什么吗?你抓住我了吗?

The below is the code:

 function createComparisonFunction(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: "Zachary", age: 28}, {name: "Nicholas", age: 29}];

    data.sort(createComparisonFunction("name")); 
    alert(data[0].name);

    data.sort(createComparisonFunction("age"));
    alert(data[0].name);

As you can see the createCompatisonFunction is filled in with value called name and as you can see inside this function, there's object1 and object2. Can I ask what does these arguments pass in with? Do you catch me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水微香 2024-11-08 09:26:28

当对 data 中的元素进行排序时,排序算法必须在两个元素之间执行基本比较,以确定哪个元素大于另一个元素。数组对象的 sort 方法提供了要比较的两个对象的比较函数。基本上你正在定义一个 <您的对象的各种运算符。

when sorting elements in data the sorting algorithm will have to perform elementary comparison between two elements to determine which one is greater than the other. The sort method of array objects, supplies the comparison function the two objects that it wants to compare. Basically you are defining a < operator of sorts for your object.

怂人 2024-11-08 09:26:28

Array.prototype.sort 接受这样定义的函数。

function(left, right) {
    return new Number();
}

排序函数传递数组的两个元素,然后返回一个数字,说明它们应该按什么顺序排列。

如果左侧元素需要在右侧之前,则只需返回 -1 ,否则如果右侧应该在左侧之前,则返回 1

如果无关紧要,则返回0

在您的情况下 object1object2{name: "Zachary",age: 28}{name: "Nicholas",年龄:29} 分别。

Array.prototype.sort accepts a function that is defined as such

function(left, right) {
    return new Number();
}

The sorting function is passed two elements of the array and you return a number stating in which order they should go.

If the left element needs to go before right then just return -1 otherwise return 1 if right is supposed to come before left.

return 0 if it does not matter.

In your case object1 and object2 are {name: "Zachary", age: 28} and {name: "Nicholas", age: 29} respectively.

橘虞初梦 2024-11-08 09:26:28

函数 createComparisonFunction 的代码是一个返回函数的函数。

本质上,您的代码与以下扩展没有什么不同:

var data = [{name: "Zachary", age: 28}, {name: "Nicholas", age: 29}];

data.sort(function(object1, object2){
  var value1 = object1["name"];
  var value2 = object2["name"];
  if(value1 < value2){
    return -1;
  }else if(value1 > value2){
    return 1;
  }else{
    return 0;
  }
}); 

alert(data[0].name);

data.sort(function(object1, object2){
  var value1 = object1["age"];
  var value2 = object2["age"];
  if(value1 < value2){
    return -1;
  }else if(value1 > value2){
    return 1;
  }else{
    return 0;
  }
});
alert(data[0].name);

创建 createComparisonFunction(propertyName) 的目的是将复制和粘贴的代码重构到一个区域,以便更轻松地管理代码。另请注意,如果您的数据对象还包含名为“lastname”的属性,则该函数也可用于按 LastName 排序。基本上,这个功能比复制和粘贴代码要强大得多。

Your code for the function createComparisonFunction is a function that returns a function.

In essence your code is no different than the following expanded out:

var data = [{name: "Zachary", age: 28}, {name: "Nicholas", age: 29}];

data.sort(function(object1, object2){
  var value1 = object1["name"];
  var value2 = object2["name"];
  if(value1 < value2){
    return -1;
  }else if(value1 > value2){
    return 1;
  }else{
    return 0;
  }
}); 

alert(data[0].name);

data.sort(function(object1, object2){
  var value1 = object1["age"];
  var value2 = object2["age"];
  if(value1 < value2){
    return -1;
  }else if(value1 > value2){
    return 1;
  }else{
    return 0;
  }
});
alert(data[0].name);

And the purpose of creating the createComparisonFunction(propertyName) is to refactor copy and pasted code into one area allowing for easier management of code. Also note if your data object also contained a propery called "lastname" this one function could also be used to sort by LastName. Basically the one function is far more robust than copying and pasting code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文