Scripting of JavaScript classes
- Defining variables and methods internally
- The this declaration is used to define common public properties and public methods
- In the internal var declaration, or simply do not write Var (implicitly declared) is the dead Yo property with private method
- An instance of a class can only access public properties and public methods
functionPet (_name,_age,_price) { This. name=_name; varAge=_age;//Private Properties varPrice=_price;//Private Properties This. Setage =function(IntAge) { age=IntAge; }/*to define a private property an external public access method for age*/ This. Getage =function() {returnAge ; } }
- Use prototype to define methods and properties on the prototype chain
- The properties and methods defined using prototype are all common
- The notation ① gives a common construction method within the class or ② directly defines the class as empty, and then writes the prototype of the class directly into the form of a JS object
Writing one:
/*define a pet class using the notation ①*/functionPet (_name, _age, _price, _color) { This. Init (_name, _age, _price, _color); Pet.prototype.name; Pet.prototype.age; Pet.prototype.price; Pet.prototype.color; Pet.prototype.init=function(_name, _age, _price, _color) {if(_name! = undefined && _age! = undefined && _price! = undefined && _color! =undefined) { This. Name =_name; This. Age =_age; This. Price =_price; This. color =_color; Document.writeln ("This.name=" + This. Name + ", this.age=" + This. Age + ", this.price=" + This. Price + ", this.color=" + This. color); }}
Two:
functionPerson2 () {}
/*constructs a class in the form of a JS object on the prototype of a class*/Person2.prototype={name:"",//Public Propertyage:0, weight:0, height:0,/*Public Method*/Init:function(_name, _age, _weight, _height) { This. Name =_name; This. Age =_age; This. Weight =_weight; This. Height =_height; Document.writeln ("This.name=" + This. Name + ", this.age=" + This. Age + ", this.weight=" + This. Weight + ", this.height=" + This. Height); }, /*Public Method*/Show:function() {Document.writeln ("Show Method"); }};
Using the above inductive characteristics, we can summarize a set of efficient and feasible writing method
- Use constructors to define the public property ,private property
- Use the prototype chain prototype to define the methods of the class (public method), and then use these methods to access public and private properties
/*define a person class*/functionPerson (_name, _age, _price) { This. Name =_name; varAge =_age; //private properties that can only be used inside a class varPrice =_price; //private properties that can only be used inside a class functionPrivatefn () {Console.log ("I am a private attribute of the Pet class age, can only be used inside the pet class, after initialization age=" +Age ); Console.log ("I am a private function of pet class Privatefn, can only be used inside the pet class"); } varPRIVATEFN2 =function() {Console.log ("I am the Pet class's private property price, can only be used inside the pet class, after initialization price=" +Price ); Console.log ("I am a private function of pet class PRIVATEFN2, can only be used inside the pet class"); } privatefn (); //calling private methods inside a pet classprivateFn2 (); //calling private methods inside a pet class}pet.prototype={setName:function(_name) { This. Name =_name; }, GetName:function () { return This. Name; }, Show:function() {Console.log ("Public Method Show"); }};
Reference: http://www.cnblogs.com/xdp-gacl/p/3700840.html
JavaScript Advanced (5)---writing class