Created
February 19, 2017 05:57
-
-
Save guerbai/b8f5cad2681f127949e3983a86ce3871 to your computer and use it in GitHub Desktop.
一个闭包,多传一个参数.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createComparisonFunciton(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(createComparisonFunciton("age")); | |
console.log(data[0].age); | |
data.sort(createComparisonFunciton("name")); | |
console.log(data[0].name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment