Summary Javascript class defines methods (rpm)

http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html

Javascript language does not support the "class", but can use some alternative methods to simulate the "class."

A constructor method

This is the classic method, but also will teach the textbook method. It uses analog constructor "class", refers to use the instance this keyword in its interior.

function Cat() {
   this.name = "大毛";
}

When generating instances, using the new keyword.

var cat1 = new Cat();
Alert (cat1.name); // ovo

Properties and methods of a class, may also be defined on the prototype object constructor.

Cat.prototype.makeSound = function(){
      Alert ( "Meow Meow" );
}

For more information about this method, see the series of articles I wrote "Javascript Object-Oriented Programming" , there is not much to say. Its main disadvantage is more complicated, and uses this prototype, write and read very laborious.

Two, Object.create () method

In order to address the shortcomings of "constructor method", more easy to generate the object, Javascript international standard ECMAScript Fifth Edition (currently prevailing in its third edition), proposed a new method Object.create () .

With this method, "class" is an object, not a function.

was Cat = {
    name: "ovo" ,
    makeSound: function(){ alert("喵喵喵"); }
};

Then, the direct use of the Object.create () to generate an instance, no need to use new.

var cat1 = Object.create(Cat);
Alert (cat1.name); // ovo 
cat1.makeSound (); // Meow Meow

Currently, the latest version of major browsers (including IE9) has deployed this method. If you have older browsers, you can use the following code on their own deployment.

if (!Object.create) {
    Object.create = function (o) {
       function F() {}
      F.prototype = o;
      return new F();
    };
}

  

This method than the "constructor method" simple, but can not be achieved and private methods Private property, between instances of objects can not share data, analog "class" is not comprehensive enough.

Third, minimalist law

Dutch programmer Gabor de Mooij proposed) better than Object.create ( new method , he said this approach as "minimalist law" (minimalist approach). This is the method I recommend.

3.1 package

This method does not use this and the prototype, the code is very simple to deploy, which is probably the reason it is called a "minimalist law".

First, it is an object simulation with "class." In this class there, CreateNew define a constructor (), to generate instances.

was Cat = {
    createNew: function(){
      // some code here
    }
  };

Then, in CreateNew () which define an instance of the object, this instance of the object as the return value.

was Cat = {
    createNew: function(){
      var cat = {};
      cat.name = "ovo" ;
      cat.makeSound = function(){ alert("喵喵喵"); };
      return cat;
    }
  };

When in use, calls createNew () method, you can get an instance of an object.

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

The advantage of this method is readily appreciated, a clear and elegant construction, in line with the conventional configuration "object-oriented programming", and therefore can be easily deployed following features.

3.2 Inheritance

Let a class inherits from another class, easy to implement. As long as the former createNew () method, call the latter createNew () method can be.

Animal define a class.

was Animal = {
    createNew: function(){
      var animal = {};
      animal.sleep = function () {Alert ( "lie" );};
      return Animal;
    }
  };

Then, in the Cat's createNew () method, call createNew Animal's () method.

was Cat = {
    createNew: function(){
      var cat = Animal.createNew();
      cat.name = "ovo" ;
      cat.makeSound = function(){ alert("喵喵喵"); };
      return cat;
    }
  };

Cat instances thus obtained, it will inherit the Cat class and the Animal class.

var cat1 = Cat.createNew();
cat1.sleep (); // sleep late

3.3 private attributes and private methods

In CreateNew () method, as long as the cat is not defined on the object's methods and properties, are private.

was Cat = {
    createNew: function(){
      var cat = {};
      var sound = "喵喵喵";
      cat.makeSound = function(){ alert(sound); };
      return cat;
    }
  };

Sound embodiment the internal variable, can not be read externally, only read by the public methods makeSound cat ().

var cat1 = Cat.createNew();
alert(cat1.sound); // undefined

3.4 Data Sharing

Sometimes, we need to all instances of an object, can read and write with an internal data. This time, as long as the internal data, encapsulated in the object class, the outside CreateNew () method can be.

was Cat = {
    Sound: "meow meow" ,
    createNew: function(){
      var cat = {};
      cat.makeSound = function(){ alert(Cat.sound); };
      cat.changeSound = function(
cat2.changeSound ( "la la la" );
cat1.makeSound (); // la la la

 

){ Cat.sound = x; };
      return cat;
    }
  };

Then, the object generates two instances:

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

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

  

(Finish)

Reproduced in: https: //www.cnblogs.com/JoannaQ/archive/2013/02/15/2912661.html

Guess you like

Origin blog.csdn.net/weixin_34393428/article/details/93058601