JavaScript Object-oriented design Function General class

Source: Internet
Author: User
Tags instance method

vartest ="Class01";functionClass01 (Privatevalue, publicvalue) {    var_this = This;    if( This. Constructor.name!==' Class01 ') { throw NewError (' class can only be instantiated '); }    / * Self-executing function to count the number of instantiations * /    (functionNewclass () {        class01.count++;/ * Statistics instantiation number of times * /        Class01.intances.push (_this);/ * Save a reference to each instance * /        Console.log (class01.count);    })();    /* Private variable and private method */    functionPrivatemethod () {Console.log (' Private method was called by Publicmethod ') } //    varprivateval = privatevalue;    / * Private Variable Storage interface * /     This. Get =function() { returnPrivateval;};     This. Set =function(v) {privateval = v;return This; };    /* Instance properties and instance methods (typically do not create an instance method, if you must define an instance method, add it yourself after instantiation) */     This. Public= Publicvalue;     This. Publicmethod =function() {        Console.log (' Public method and then call Private method ');        Privatemethod (); / * Use Private method internally * /        return This;    };    /* Instance methods can use the static properties and methods of the class */     This. Callclassstaticmethod =function() {        Console.log (class01.staticvalue);        Class01.staticmethod ();    };}/* Prototype properties and Methods * 1, the class is instantiated and the array that holds the reference can also be placed in the prototype * 2, each instance has references and instantiation times for other instances */Class01.prototype.proValue =' This is Class01 prototype value ';Class01.prototype.proMethod =function() {    Console.log (' This is Class01 prototype method ');};Class01.prototype.proArray = [1, 2, 3, 4, 5];Class01.prototype.proObject = {a:' A ' };/* Static properties and Static methods * Whether a static property can be used to store the number of times the class is instantiated by adding a self-executing function to the class.  * You can also perform some action at each instantiation * 1, for example, when the number of instances of more than a certain value, throw an error, tell the program memory consumption is too high.  * 2, in the static properties of the class, you can save a reference to each instance (which will definitely cause a memory leak).  */Class01.staticvalue =' This is class static value ';Class01.staticmethod =function() {Console.log (' This is class static method ') };Class01.count =0;class01.intances = [];/ * Test Class01 * /off(Test = = ="Class01") {    varinstance01 =NewClass01 (' Private value1 ', ' public value1 ');    varINSTANCE02 =NewClass01 (' Private value2 ', ' public value2 ');    Console.log (class01.intances);    Console.log (' Instance private attribute ');    Console.log (Instance01.get ());    Console.log (Instance01.set (' Change private value1 '). get ());    Console.log (Instance02.get ());    Console.log (Instance02.set (' Change private value2 '). get ());    Console.log (' Instance Properties ');    Console.log (instance01. Public);    Console.log (instance02. Public);    Console.log (' instance method ');    Instance01.publicmethod ();    Instance02.publicmethod ();    Console.log (Instance01.publicmethod = = = Instance02.publicmethod);    Console.log (' Instance prototype properties ');    Console.log (instance01.provalue);    Instance01.provalue =' instance01 change proto value ';    Console.log (instance01.provalue);    Console.log (instance02.provalue);    /*INSTANCE01 has not been able to modify the prototype properties, but instead created instance properties on the instance * /    Try{        / * Cannot change the properties on the prototype on the instance * /        Instance01.prototype.proValue =' class static value changed ';    } Catch(e) {        Console.error (e);    }    Try{        /* In summary instances do not allow prototype to use properties and methods */        Console.log (instance02.prototype.proValue);    } Catch(e) {        Console.error (e);    }    / * If the prototype attribute is a numeric/string/Boolean value, the instance is no means can be modified. When a prototype property is a reference (a value/object), it can be modified so that it reacts on all instance objects. */    Console.log (instance01.proarray);    Instance01.proArray.push (6);    Console.log (instance02.proarray);    Console.log (' class static method ');    Instance01.callclassstaticmethod ();    Instance02.callclassstaticmethod ();    Try{        /* Cannot set the prototype method on the instance (the reason is simple, more than 10 people do a project, everyone wants to add a method to the instance prototype, the project will not be completed) */        Instance01.prototype.print =function() {            Console.log (' prototype ');        };    } Catch(e) {        Console.error (e);    }    Try{        / * Although Class01 is function but cannot be executed * /        Class01 ();    } Catch(e) {        Console.error (e);    }    Try{        / * Obviously, you can't use the call method. */        varinstance03 = {};        Class01.call (INSTANCE03,' Private value3 ', ' public value3 ');    } Catch(e) {        Console.error (e);    }    / * Following this method, you can use CLASS01 for functional "instantiation", but the prototypes are gone. */    functionclass01_t () {    }    Class01_t.prototype.constructor = {name:' Class01 '};    varinstance04 =Newclass01_t ();    Class01.call (INSTANCE04,' Private value4 ', ' public value4 ');    Console.log (INSTANCE04);    / * The following method can perfectly simulate Class01 instantiation     * 1, the following example can be seen, in Class01_t2, you can add an instance of new properties and methods.      * See below from the perspective of inheritance.      * 2, Class01_t2 defined in the properties and methods, obviously will be Class01 in the cover out.      * 3. Adding methods and attributes to the CLASS01_T2 prototype will obviously overwrite the Class01. Thus affecting all instances created by Class01     * 4, anyway, Class01 static methods and properties are in constructor     * The current topic is not the inheritance of classes, and the instantiation of the function class and related knowledge are introduced so much at present.      */    functionclass01_t2 () {        Console.log (' class01_t2 was called ');        Class01.call ( This, ' Private value5 ', ' public value5 ');        / * At this point, you can hijack Class01 's public properties and methods, and cannot hijack private properties and methods (cannot get its reference and cannot be called naturally).          * Methods of hijacking can be implemented using the Function.prototype.after method.          */    }    class01_t2.prototype = Class01.prototype;    varinstance05 =Newclass01_t2 ();    Console.log (instance05);    Instance05.constructor.staticMethod ();    Console.log (Instance05.constructor = = = Class01);    / * constructor points to class01;*/    Console.log (instance05.prototype);    /*undefined*/    Console.log (instance04.prototype);    /*undefined*/    Console.log (instance01.prototype);    /*undefined*/    Console.log (Class01_t2.constructor = = = Class01.constructor);    / * Constructors equal * /    Console.log (class01_t2.__proto__ = = = class01.__proto__);    /*true*/    Console.log (instance05.__proto__ = = = instance01.__proto__);    /*true*/    Console.log (instance05.constructor.name);    /*class01*/    / * The class can be found through the constructor of the instance. You can instantiate from a class by using new constructor. */    Console.log (Instance05.__proto__.constructor = = = Class01);    /*true*/    Console.log (Instance05.constructor = = = Class01);    /*true*/    varinstance06 =NewInstance05.constructor (' Private value6 ', ' public value6 ');    Console.log (Instance06.get ());    / * Summary     * 1, only the function can be instantiated, the variables obtained after instantiation is an instance is not a function, or an object     * 2, there is a way that can not modify Class01 can also expand the variable or method (public or private), the above Class01_t2     * For this way, quite a lot of content can be imagined: for example, whether you can hijack public methods in Class01     */}

JavaScript Object-oriented design Function General class

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.