Returning a function from a function
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/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: "XML", pageSize: 28}, {name: "JavaScript", pageSize: 29}];
data.sort(createComparisonFunction("name"));
document.writeln(data[0].name); //JavaScript
data.sort(createComparisonFunction("pageSize"));
document.writeln(data[0].name); //XML
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Function:
- The Function Type
- Function Declarations versus Function Expressions
- Functions as Values
- Returning a function from a function
- Function arguments
- this for function context
- Function caller
- Function length property
- Function apply()
- Function.call() method
- Function's bind() method
- Function toLocaleString() and toString()