Advertisement
TlalocTev

JS extend 2

Mar 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Object.defineProperty(Object.prototype,
  2.     "extend",                  // Define Object.prototype.extend
  3.     {
  4.         writable: true,
  5.         enumerable: false,     // Make it nonenumerable
  6.         configurable: true,
  7.         value: function(o) {   // Its value is this function
  8.             // Get all own props, even nonenumerable ones
  9.             var names = Object.getOwnPropertyNames(o);
  10.             // Loop through them
  11.             for(var i = 0; i < names.length; i++) {
  12.                 // Skip props already in this object
  13.                 if (names[i] in this) continue;
  14.                 // Get property description from o
  15.                 var desc = Object.getOwnPropertyDescriptor(o,names[i]);
  16.                 // Use it to create property on this
  17.                 Object.defineProperty(this, names[i], desc);
  18.             }
  19.         }
  20.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement