Minimalist approach Dutch programmer Gabor de Mooij proposed a new method that is better than Object.create (). He called this method "minimalist approach" . This is also the method I recommend.
3. 1 Encapsulation
This method does not use this and prototype, and the code is very simple to deploy. This is probably why it is called the "minimalist method".
First of all, it also uses an object to simulate a "class". In this class, define a constructor createNew () to generate instances.
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 = "大马";
cat.makeSound = function (){ alert ("Meow Meow"); };
return cat;
}
};
When using it, call the createNew () method to get the instance object.
var cat1 = Cat.createNew ();
cat1.makeSound (); // Meow Meow Meow
The advantage of this method is that it is easy to understand, has a clear and elegant structure, and conforms to the traditional "object-oriented programming" structure, so it can be easily deployed Features below.
3. 2 Inheritance
It is very convenient to let one class inherit another class. Just call the latter's createNew () method in the former's createNew () method.
First define an Animal class.
var Animal = {
createNew: function () {
var animal = {};
animal.sleep = function (){ alert ("Sleep in"); };
return animal;
}
};
Then, in Cat’s createNew () method, call Animal’s createNew () method.
var Cat = {
createNew: function () {
var cat = Animal.createNew ();
cat.name = "大毛";
cat.makeSound = function (){ alert ("Meow meow"); };
return cat;
}
};
The Cat instance obtained in this way will inherit both the Cat class and the Animal class.
var cat1 = Cat.createNew ();
cat1.sleep (); // Sleeping in
3. 3 Private properties and private methods
In the createNew () method, as long as it is not defined on the cat object Methods and properties are private.
var Cat = {
createNew: function () {
var cat = {};
var sound = "meow meow";
cat.makeSound = function (){ alert (sound); };
return cat;
}
};
The internal variable sound in the above example cannot be read externally and can only be read through the public method makeSound () of cat.
var cat1 = Cat.createNew ();
alert (cat1.sound); // undefined
3. 4 Data Sharing
Sometimes, we need all instance objects to be able to read and write the same internal data. At this time, just encapsulate the internal data inside the class object and outside the createNew () method.
var Cat = {
sound : "Meow meow",
createNew: function (){
var cat = {};
cat.makeSound = function (){ alert (Cat.sound); };
cat.changeSound = function (x){ Cat.sound = x; };
return cat;
}
};
Then, generate two instance objects:
var cat1 = Cat.createNew ();
var cat2 = Cat.createNew ();
cat1.makeSound (); // Meow Meow Meow
At this time, if there is an instance Object, if the shared data is modified, another instance object will also be affected.
cat2.changeSound ("La La La");
cat1.makeSound (); // la la la