Advertisement
TlalocTev

JS extend 1

Mar 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copy the enumerable properties of p to o, and return o.
  3.  * If o and p have a property by the same name, o's property is overwritten.
  4.  * This function does not handle getters and setters or copy attributes.
  5.  */
  6. function extend(o, p) {
  7.     for(prop in p) {                         // For all props in p.
  8.         o[prop] = p[prop];                   // Add the property to o.
  9.     }
  10.     return o;
  11. }
  12.  
  13. /*
  14.  * Copy the enumerable properties of p to o, and return o.
  15.  * If o and p have a property by the same name, o's property is left alone.
  16.  * This function does not handle getters and setters or copy attributes.
  17.  */
  18. function merge(o, p) {
  19.     for(prop in p) {                           // For all props in p.
  20.         if (o.hasOwnProperty[prop]) continue;  // Except those already in o.
  21.         o[prop] = p[prop];                     // Add the property to o.
  22.     }
  23.     return o;
  24. }
  25.  
  26. /*
  27.  * Remove properties from o if there is not a property with the same name in p.
  28.  * Return o.
  29.  */
  30. function restrict(o, p) {
  31.     for(prop in o) {                         // For all props in o
  32.         if (!(prop in p)) delete o[prop];    // Delete if not in p
  33.     }
  34.     return o;
  35. }
  36.  
  37. /*
  38.  * For each property of p, delete the property with the same name from o.
  39.  * Return o.
  40.  */
  41. function subtract(o, p) {
  42.     for(prop in p) {                         // For all props in p
  43.         delete o[prop];                      // Delete from o (deleting a
  44.                                              // nonexistent prop is harmless)
  45.     }
  46.     return o;
  47. }
  48.  
  49. /*
  50.  * Return a new object that holds the properties of both o and p.
  51.  * If o and p have properties by the same name, the values from o are used.
  52.  */
  53. function union(o,p) { return extend(extend({},o), p); }
  54.  
  55. /*
  56.  * Return a new object that holds only the properties of o that also appear
  57.  * in p. This is something like the intersection of o and p, but the values of
  58.  * the properties in p are discarded
  59.  */
  60. function intersection(o,p) { return restrict(extend({}, o), p); }
  61.  
  62. /*
  63.  * Return an array that holds the names of the enumerable own properties of o.
  64.  */
  65. function keys(o) {
  66.     if (typeof o !== "object") throw TypeError();  // Object argument required
  67.     var result = [];                 // The array we will return
  68.     for(var prop in o) {             // For all enumerable properties
  69.         if (o.hasOwnProperty(prop))  // If it is an own property
  70.             result.push(prop);       // add it to the array.
  71.     }
  72.     return result;                   // Return the array.
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement