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>
  
Click to view the demo
Home 
  JavaScript Book 
    Essential Types  

Function:
  1. The Function Type
  2. Function Declarations versus Function Expressions
  3. Functions as Values
  4. Returning a function from a function
  5. Function arguments
  6. this for function context
  7. Function caller
  8. Function length property
  9. Function apply()
  10. Function.call() method
  11. Function's bind() method
  12. Function toLocaleString() and toString()