Answer the question
In order to leave comments, you need to log in
How to use the method of copying object properties with their attributes?
I'm reading Flanagan now. I came across a very useful script, but I can't figure out how to use its code to copy object properties (including their attributes). Give a simple example, please! (code below)
Example 6.3. Copy property attributes
/*
* Adds a non-enumerable extend() method to Object.prototype.
* This method extends objects with the ability to copy properties from the object
* passed in the argument. This method copies not only the value of the properties
* but also all of their attributes. All own *
properties (even those that are not enumerable), except for those of the same name
* properties available in the current object.
*/
Object.defineProperty(Object.prototype,
"extend", // Defined Object.prototype.extend
{
writable: true,
enumerable: false, // Make it non-enumerable
configurable : true, value:
function(o) { // Property value is this function
// Get all own properties, even non-enumerable ones var names
= Object.getOwnPropertyNames(o); //
Loop through them in a for(var i =
0; i < names.length; i++) { //
Skip properties, already available in this object
if (names[i] in this) continue;
// Get property descriptor from o
var desc = Object.getOwnPropertyDescriptor(o,names[i]);
// Use it to create a property on the given object
Object.defineProperty (this, names[i], desc);
}
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question