JS Learning Object Creation

Source: Internet
Author: User

Object.extend=function(destination, source) {
for(varPropertyinchSOURCE) {
Destination[property] = Source[property];
} Prototype the object class is extended primarily through a static function Object.extend(destination, source) implements the JavaScriptInheritance in the. From a semantic point of view, Object.extendThe (destination, source) method is somewhat illogical because it actually only implements a holographic copy from the source object to the target object. But you can also think of this: because the target object has all the attributes owned by the source object, it looks like the target object inherits the source object (and expands it). In addition, prototype extends several more useful static methods to object, and all other classes can obtain support by invoking these static methods.

JScript Code

Object.extend=function(destination, source) {//A static method represents inheritance, and the target object will have all the properties and methods of the source object
for(varPropertyinchSOURCE) {
Destination[property] = Source[property];//Dynamically adding properties and methods by assigning values using the characteristics of dynamic languages
returnDestination//returning an extended object

Object.extend(Object, {
Inspectfunction(object) {//a static method that passes in an object that returns a string representation of the object
if(object = = undefined)return' Undefined ';//Handling undefined situations
if(Object = =NULL)return' NULL ';//Handling NULL Cases
//if the object defines the inspect method, call the method to return, otherwise return the object's ToString () value
returnObject.inspect? Object.inspect (): Object.ToString ();
}Catch(e) {
if(EinstanceofRangeerror)return‘...‘;//Handling Exception Conditions
Keysfunction(object) {//a static method that passes in an object that returns all of the properties of the object, constituting the array returned
varKeys = [];
Keys.push (property);//press each property into an array
Valuesfunction(object) {//a static method that passes in an object that returns the values of all the properties in the object, constituting the array returned
varvalues = [];
for(varPropertyinchObject) Values.push (Object[property]);//presses the value of each property into an array
Clonefunction(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.