Section 8.7. Utility Function Examples


8.7. Utility Function Examples

This section includes a few useful example functions for manipulating objects, arrays, and functions. Let's begin with some object utilities in Example 8-3.

Example 8-3. Object utility functions

 // Return an array that holds the names of the enumerable properties of o function getPropertyNames(/* object */o) {     var r = [];     for(name in o) r.push(name);     return r; } // Copy the enumerable properties of the object from to the object to. // If to is null, a new object is created.  The function returns to or the // newly created object. function copyProperties(/* object */ from, /* optional object */ to) {     if (!to) to = {};     for(p in from) to[p] = from[p];     return to; } // Copy the enumerable properties of the object from to the object to, // but only the ones that are not already defined by to. // This is useful, for example, when from contains default values that // we want to use if they are not already defined in to. function copyUndefinedProperties(/* object */ from, /* object */ to) {     for(p in from) {         if (!p in to) to[p] = from[p];     } } 

Next, here are some array utilities in Example 8-4.

Example 8-4. Array utility functions

 // Pass each element of the array a to the specified predicate function. // Return an array that holds the elements for which the predicate // returned true function filterArray(/* array */ a, /* boolean function */ predicate) {     var results = [];     var length = a.length;  // In case predicate changes the length!     for(var i = 0; i < length; i++) {         var element = a[i];         if (predicate(element)) results.push(element);     }     return results; } // Return the array of values that result when each of the elements // of the array a are passed to the function f function mapArray(/* array */a, /* function */ f) {     var r = [];             // to hold the results     var length = a.length;  // In case f changes the length!     for(var i = 0; i < length; i++) r[i] = f(a[i]);     return r; } 

Finally, the functions in Example 8-5 are utilities for working with functions. They actually use and return nested functions. Nested functions returned in this way are sometimes called closures, and they can be confusing. Closures are discussed in the next section.

Example 8-5. Utility functions for functions

 // Return a standalone function that invokes the function f as a method of // the object o.  This is useful when you need to pass a method to a function. // If you don't bind it to its object, the association will be lost and // the method you passed will be invoked as a regular function. function bindMethod(/* object */ o, /* function */ f) {     return function() { return f.apply(o, arguments) } } // Return a function that invokes the function f with the // specified arguments and also any additional arguments that are // passed to the returned function. (This is sometimes called "currying".) function bindArguments(/* function */ f /*, initial arguments... */) {     var boundArgs = arguments;     return function() {         // Build up an array of arguments.  It starts with the previously         // bound arguments and is extended with the arguments passed now         var args = [];         for(var i = 1; i < boundArgs.length; i++) args.push(boundArgs[i]);         for(var i = 0; i < arguments.length; i++) args.push(arguments[i]);         // Now invoke the function with these arguments         return f.apply(this, args);     } } 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net