Pleanders Writing JavaScript class

This so-called “minimalism” is proposed by the Dutch programmer Gabor de Mooij. This method does not use this and prototype, the code is deployed very simple, which is probably it is called “extreme briefism” s reason. Here is how to use minimalism to complete JavaScript packages and inheritance

First, it is also an object to simulate “class”. In this class, define a constructor createNew () to generate an instance.

VAR CAT = {CreateNew: function () {// Some code Here}};
   
Then, In CreateNew (), define an instance object and use this instance object as the return value.

VAR CAT = {CreateNew: Function () {var Cat = {}; Cat.Name = “Machi”; Cat.makesound = Function () {Alert (“喵 喵 喵 “);}; return cat;}};
When using the CreateNew () method, an instance object can be obtained.
   VAR CAT1 = Cat.createNew (); Cat1.makesound (); // 喵 喵 喵 

The advantage of this method is, it is easy to understand, the structure is clear and elegant, which is in line with the traditional “object-oriented programming”.Therefore, the following characteristics can be conveniently deployed.
2. Inherit
   Let a class inherit another class, which is very convenient to achieve. As long as in the CreateNew () method of the former, the latter's CreateNew () method can be called. 
First define an Animal class:

var Animal = {CreateNew: function () {var Animal = {}; Animal.Sleep = function () {Alert “Sleeping lazy”);}; return animal;}};

Then, in the CREATENEW () method of CAT, animal CreateNew () method

VAR CAT = {CreateNew: Function () {var Cat = Animal.createNew (); Cat.Name = “big hair”; cat.makessound = function () {Alert (“喵 喵 “);}; return cat;}};

The resulting CAT instance will inherit animal class.
 VAR CAT1 = Cat.createNew (); Cat1.Sleep (); // Sleeping   

3. Private properties and private methods

in CrIn the EateNew () method, it is privately available as long as it is not defined on the CAT object.
   VAR CAT = {CreateNew: Function () {var catch = {}; var Sound = "喵 喵"; // Private property Cat.makesound = function ) {Alert;}; return cat;}}; 
The internal variable SOUND of the previous example, the outside cannot be read, only the public method MAKESOUND passing through CAT ( ) To read.

VAR CAT1 = Cat.createNew (); Alert (Cat1.Sound); // Undefined
   
4. Data sharing

Sometimes we need all instance objects to read and write the same internal data. At this time, as long as this internal data, the encapsulation can be encapsulated inside the class object, it can be outside the CreateNew () method.

VAR CAT = {Sound: “喵”, CreateNew: function () {var cat = {}; cat.makesound = function () {alert (Cat . Sound;}; catch (x) {cat.sound = x;}; return cat;}};

  Then, the object generates two instances:  

var cat1 = Cat.createNew (); var cat2 = Cat.createNew (); cat1.makeSound (); // meow meow

  At this time, if there is an instance of the object, modify the shared data, another instance of the object will also be affected.  

cat2.changeSound ( “la la la”); cat1.makeSound (); // la la la

minimalism, looks very nice, but there are drawbacks, above all, can not be used to determine class instanceof object belongs, “cat1 instanceof Cat” can not, in addition, minimalism emerged from the disadvantage of using the prototype chain (attributes can not private, create, objects that inherit not intuitive), but also exposed the shortcomings of the prototype chain useless: every time you build one instance, both must be duplicate content, multiple occupy some memory.
 summary   
The above method is to write minimalist small series to introduce JavaScript class, we want to help, if you have any questions please Leave me a message, Xiao Bian will promptly reply to everyone. Thank you very much for your support of Tumi Cloud Website!
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment