Javascript object-oriented (2) code encapsulation. If you need code, refer to the following example:
Step 1: create a "Mobile Phone type"
var MobilePhone = (function(){ ………… })()
Step 2: Consider the private attributes of these classes. Here I want to define the number of mobile phones that come out of the instance.
Var MobilePhone = (function () {// Private Attribute var count = 0; // represents the number of mobile phones })()
Step 3: Create a constructor, that is, to initialize the new image, such as attribute and method initialization. In this example, each mobile phone will have a color, size, price attribute. here the constructor is also a closure, so you can access count and the value of count will be stored in the memory for a long time (as long as there is a reference)
Var MobilePhone = (function () {// Private Attribute var count = 0; // represents the number of mobile phones. // constructor var creatphone = function (color, size, price) {count ++; this. _ color = color; // the color of the phone. this. _ size = size; // cell phone size this. _ price = price; // mobile phone price this. index = count; // mobile phone index, which is the number of mobile phone photos created }})()
Step 4: common methods:
That is, all the mobile phone objects from the instance can be used. Here, the mobile phone should be able to change the price, color, size, or display the size, color, and price.
The common methods here should be placed in the "prototype object:
1. All objects created using the constructor instance, that is, mobile phones, can use the methods in the "prototype object.
2. If it is placed in the constructor, a bunch of repeated methods will be generated every time a mobile phone object is displayed on the instance, occupying the memory.
3. "constructor": creatphone explanation:
Because creatphone. prototype = {......} It overwrites the reference of the previous prototype object. To associate the prototype object with the constructor, set "constructor": creatphone.
Var MobilePhone = (function () {// Private Attribute var count = 0; // represents the number of mobile phones. // constructor var creatphone = function (color, size, price) {count ++; this. _ color = color; // the color of the phone. this. _ size = size; // cell phone size this. _ price = price; // mobile phone price this. index = count; // mobile phone index, which is the number of mobile phone mobile phones created} // public method, which is stored in the prototype object creatphone. prototype = {"constructor": creatphone, // obtain the phone color "getColor": function () {return this. _ color ;}, // set the cell phone color "setColor": function (color) {this. _ color = color ;}, // get the phone size "getSize": function () {return "width:" + this. _ size. width + "height:" + this. _ size. height ;}, // set the cell phone size "setSize": function (size) {this. _ size. width = size. width; this. _ size. height = size. height ;}, // get the mobile phone price "getPrice": function () {return this. _ price ;}, // set the mobile phone price "setPrice": function (price) {this. _ price = price }}})()
Step 5: The privileged method requires a method that can be used to remove the private variables of the privileged class. It is the number of mobile phone objects that come out of the instance.
Var MobilePhone = (function () {// Private Attribute var count = 0; // the number of mobile phones var index = 0; // indicates the index of the mobile phone. // constructor var creatphone = function (color, size, price) {count ++; this. _ color = color; // the color of the phone. this. _ size = size; // cell phone size this. _ price = price; // mobile phone price this. index = count; // mobile phone index, which is the number of mobile phone mobile phones created} // public method, which is stored in the prototype object creatphone. prototype = {"constructor": creatphone, "getColor": function () {return this. _ color;}, "setColor": function (color) {this. _ color = color;}, "getSize": function () {return "width:" + this. _ size. width + "height:" + this. _ size. height;}, "setSize": function (size) {this. _ size. width = size. width; this. _ size. height = size. height;}, "getPrice": function () {return this. _ price ;}, "setPrice": function (price) {this. _ price = price }}// privileged method creatphone. get_count_index = function () {return count} return creatphone ;})()
Use a mobile phone test encapsulated above
Var anycall = new MobilePhone (); // instance a Samsung mobile phone object var HTC = new MobilePhone (); // instance an HTC mobile phone object var Iphone4s = new MobilePhone (); // instance an apple 4S mobile phone object console. log ("Samsung:" + anycall. index + ""); // FF console outputs the number of Samsung mobile phone objects created, that is, the index; console. log ("HTC is:" + HTC. index + ""); // FF console outputs the number of HTC mobile phone objects created, that is, the index; console. log ("Iphone4s is the number:" + Iphone4s. index + ""); // FF console outputs the number of Apple 4S mobile phone objects created, that is, the index; console. log ("created in total" + MobilePhone. get_count_index () + "cell phone"); // Several cell phones are created in the output of the FF console. log (anycall. constructor === MobilePhone); // whether the constructor in the original image of the instance also points to MobilePhone
The results are as follows: