Minimalist law
Dutch programmer Gabor de Mooij proposed a new approach that was better than (), which he called "minimalist approach". This is also the method I recommend.
3. 1 Package
This method does not use this and prototype, and the code is very simple to deploy, which is probably why it is called "minimalist law".
First of all, it also uses 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 = {};
= "big hair";
= function (){ alert ("Meow Meow Meow"); };
return cat;
}
};
When using it, call the createNew () method to get the instance object.
var cat1 = ();
(); // Meow Meow Meow Meow
The advantage of this approach is that it is easy to understand, has a clear and elegant structure, and is in line with the traditional "object-oriented programming" construction, so the following features can be easily deployed.
3. 2 Inheritance
It is very convenient to implement one class in the next class. Just call the createNew () method of the latter in the createNew () method.
First define an Animal class.
var Animal = {
createNew: function (){
var animal = {};
= function (){ alert ("Sleep in"); };
return animal;
}
};
Then, in Cat's createNew () method, call Animal's createNew () method.
var Cat = {
createNew: function (){
var cat = ();
= "big hair";
= function (){ alert ("Meow Meow Meow"); };
return cat;
}
};
The Cat instance obtained in this way will inherit both the Cat class and the Animal class.
var cat1 = ();
(); // Sleep in
3. 3 Private attributes and private methods
In the createNew () method, as long as the methods and properties not defined on the cat object are private.
var Cat = {
createNew: function (){
var cat = {};
var sound = "Meow Meow Meow";
= function (){ alert (sound); };
return cat;
}
};
The internal variable sound in the above example cannot be read externally, and can only be read through cat's public method makeSound ().
var cat1 = ();
alert (); // 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 this internal data inside the class object and outside the createNew () method.
var Cat = {
sound : "Meow Meow Meow",
createNew: function (){
var cat = {};
= function (){ alert (); };
= function (x){ = x; };
return cat;
}
};
Then, two instance objects are generated:
var cat1 = ();
var cat2 = ();
(); // Meow Meow Meow Meow
At this time, if there is an instance object and the shared data is modified, the other instance object will also be affected.
("Lalala");
(); // Lalala
Dutch programmer Gabor de Mooij proposed a new approach that was better than (), which he called "minimalist approach". This is also the method I recommend.
3. 1 Package
This method does not use this and prototype, and the code is very simple to deploy, which is probably why it is called "minimalist law".
First of all, it also uses an object to simulate "class". In this class, define a constructor createNew () to generate an instance.
Copy the codeThe code is as follows:
var Cat = {
createNew: function (){
// some code here
}
};
Then, in createNew (), define an instance object and use this instance object as the return value.
Copy the codeThe code is as follows:
var Cat = {
createNew: function (){
var cat = {};
= "big hair";
= function (){ alert ("Meow Meow Meow"); };
return cat;
}
};
When using it, call the createNew () method to get the instance object.
Copy the codeThe code is as follows:
var cat1 = ();
(); // Meow Meow Meow Meow
The advantage of this approach is that it is easy to understand, has a clear and elegant structure, and is in line with the traditional "object-oriented programming" construction, so the following features can be easily deployed.
3. 2 Inheritance
It is very convenient to implement one class in the next class. Just call the createNew () method of the latter in the createNew () method.
First define an Animal class.
Copy the codeThe code is as follows:
var Animal = {
createNew: function (){
var animal = {};
= function (){ alert ("Sleep in"); };
return animal;
}
};
Then, in Cat's createNew () method, call Animal's createNew () method.
Copy the codeThe code is as follows:
var Cat = {
createNew: function (){
var cat = ();
= "big hair";
= function (){ alert ("Meow Meow Meow"); };
return cat;
}
};
The Cat instance obtained in this way will inherit both the Cat class and the Animal class.
Copy the codeThe code is as follows:
var cat1 = ();
(); // Sleep in
3. 3 Private attributes and private methods
In the createNew () method, as long as the methods and properties not defined on the cat object are private.
Copy the codeThe code is as follows:
var Cat = {
createNew: function (){
var cat = {};
var sound = "Meow Meow Meow";
= function (){ alert (sound); };
return cat;
}
};
The internal variable sound in the above example cannot be read externally, and can only be read through cat's public method makeSound ().
Copy the codeThe code is as follows:
var cat1 = ();
alert (); // 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 this internal data inside the class object and outside the createNew () method.
Copy the codeThe code is as follows:
var Cat = {
sound : "Meow Meow Meow",
createNew: function (){
var cat = {};
= function (){ alert (); };
= function (x){ = x; };
return cat;
}
};
Then, two instance objects are generated:
Copy the codeThe code is as follows:
var cat1 = ();
var cat2 = ();
(); // Meow Meow Meow Meow
At this time, if there is an instance object and the shared data is modified, the other instance object will also be affected.
Copy the codeThe code is as follows:
("Lalala");
(); // Lalala