object. extend = function (destination, source) {
for ( var property in source) {
destination [property] = source [property];
}< br> return destination;
} Prototype extends object classes mainly through a static function
Object. Extend(Destination, source) Implements
Javascript. From the semantic point of view,
Object. ExtendThe (destination, source) method is somewhat non-logical, because it only implements holographic copying from the source object to the target object. However, you can also think that because the target object has all the features of the source object, it looks like the target object inherits the source object (and extends it. In addition, prototype extends several useful static methods for objects. All other classes can obtain support by calling these static methods.
JScript code
Object. Extend = Function (Destination, source ){ // A static method indicates inheritance. The target object will have all the attributes and methods of the source object.
For ( VaR Property In Source ){
Destination [property] = Source [property]; // Dynamically add attributes and methods by assigning values based on Dynamic Language Features
}
Return Destination; // Returns the extended object.
}
Object. Extend (Object ,{
Inspect: Function (Object ){ // A static method that passes in an object and returns the string representation of the object.
Try {
If (Object = Undefined) Return ' Undefined ' ; // Undefined handling
If (Object = Null ) Return ' Null ' ; // Handle null Cases
// If the object defines the inspect method, this method is called to return, otherwise the tostring () value of the object is returned.
Return Object. Inspect ? Object. Inspect (): object. tostring ();
} Catch (E ){
If (E Instanceof Rangeerror) Return ' ... ' ; // Handle exceptions
Throw E;
}
},
Keys: Function (Object ){ // A static method is used to input an object and return all attributes of the object.
VaR Keys = [];
For ( VaR Property In Object)
Keys. Push (property ); // Press each attribute into an array.
Return Keys;
},
Values: Function (Object ){ // A static method is used to input an object and return the values corresponding to all attributes of the object.
VaR Values = [];
For ( VaR Property In Object) values. Push (object [property]); // Press the value of each attribute into an array.
Return Values;
},
Clone: Function (Object ){ // A static method, passing in an object, cloning a new object, and returning
Return Object. Extend({}, Object );
}
});