为什么如果我将对象传递给函数,我不需要在 JavaScript 中声明函数参数
下面是代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当对
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. Thesort
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.Array.prototype.sort 接受这样定义的函数。
排序函数传递数组的两个元素,然后返回一个数字,说明它们应该按什么顺序排列。
如果左侧元素需要在右侧之前,则只需返回
-1
,否则如果右侧应该在左侧之前,则返回1
。如果无关紧要,则返回
0
。在您的情况下
object1
和object2
是{name: "Zachary",age: 28}
和{name: "Nicholas",年龄:29}
分别。Array.prototype.sort
accepts a function that is defined as suchThe 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 return1
if right is supposed to come before left.return
0
if it does not matter.In your case
object1
andobject2
are{name: "Zachary", age: 28}
and{name: "Nicholas", age: 29}
respectively.函数
createComparisonFunction
的代码是一个返回函数的函数。本质上,您的代码与以下扩展没有什么不同:
创建
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:
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.