This article describes how you can use JavaScript to encapsulate a class that you don't know about using JavaScript to encapsulate a class, or if you're interested in using JavaScript to encapsulate a class, so let's take a look at this article, okay?
Have learned other object-oriented language Javascripter, may have applied classes, such as: class{}, and other definitions of a series of methods,
But beginners look at the time to learn JS, often see such a word, that is, JavaScript is object-oriented language, but no matter how they learn, are not very clear object-oriented programming, I am so, began to tangle JS object-oriented programming, these days is somewhat understood, Talk about my understanding of JS class ...
The so-called class, will have the following functions:
1. Constructors
2. Static property, static method
3. Common attributes, common method
4. Private property, Private method
This article is about how to use JS to implement the encapsulation of classes, to achieve the above functions,
1. A simple class
var person = function (name, age) { this.name = name; This.age = age; This.sayname = function () { console.log (this.name); };}
How do you think TA is not like a class then you can do that
var person = function (name, age) { //Common attribute this.name = name; This.age = age; Common method This.sayname = function () { console.log (this.name);} ;}
If the constructor mode is not very clear, you can see here JS Create object design mode
2. A complex class
With the above example, we can do our perfect on this basis.
var person = function (name, age) { //Common attribute this.name = name; Common method This.sayname = function () { console.log (this.name); }; Static Private property (only for internal calls) var home = "China"; Static Private method (only for internal calls) function Sayhome () { console.log (home); } Constructor this.setage = function (age) { Console.log (age +); }; This.setage (age);} static method (accessible only by Class) Person.sayage = function () { Console.log ("Your is 12");} Static properties (accessible only by Class) Person.drink = "water";//Static common methods (both classes and instances can be accessed) Person.prototype.sayWord = function () { Console.log (" Ys is a boy ");
JS in the use of the above simulation method, the realization of the creation of the class, on this basis, we are uneasy about the status quo, want to encapsulate him, let him become a whole, more conducive to the embodiment of the packaging of JS.
3. Package JS class
Here we use closures to implement, first explaining the concept of closure.
Closure concept: One function has access to a variable in another function scope, that is, creating another function inside one function
The implementation is as follows:
var person = (function () { ///Static Private property method var home = "China"; function Sayhome (name) { Console.log (name + "s home in" + Home); } constructor function _person (name, age) { var _this = this; Constructor security mode, avoid creating time to discard the new keyword if (_this instanceof _person) { //Common attribute, method _this.name = name; _this.gethome = function () { //internal access to private property, method Sayhome (_this.name); }; _this.test = Sayhome; For test //builder _this.setage = function (age) { _this.age = age +; } (age); } else{ return new _person (name, age); } } Static Common Property Method _person.prototype = { constructor: _person, drink: "Water", sayword:function () { Console.log ("Ys is a Boy"); } } return _person;}) ();
The call is as follows:
var p1 = new Person ("Ys", N);p 1.getHome (); Ys ' s home in Chinaconsole.log (p1.age); var P2 = person ("ys",;p 2.getHome); Ys ' s home in Chinaconsole.log (p2.age); Console.log (p2.test = = p1.test); True to prove the sharing of static private variables
Like the code above, we use JS to implement the class
Summarize:
1, some public properties, methods, can be set to static, so that at each instantiation, you do not need to open up memory resources, to achieve a true sense of sharing,
2, some public property methods, only want to be in the internal program processing time to achieve the share, then set to, static private property method,
3, some public property methods, want to achieve sharing in the instance object, then set to the prototype property method,
The above is all the content of this article, if you are not too familiar with the words, you can realize the two sides are easy to master Oh!
Related recommendations:
Various ways to do JavaScript encapsulation
JavaScript encapsulation Phone Class function code example detailed