JavaScript Object. Extend usage

Source: Internet
Author: User
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 );
}
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.