JavaScript Advanced (5)---writing class

Source: Internet
Author: User

Scripting of JavaScript classes

    • Defining variables and methods internally
  1. The this declaration is used to define common public properties and public methods
  2. In the internal var declaration, or simply do not write Var (implicitly declared) is the dead Yo property with private method
  3. 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
    1. The properties and methods defined using prototype are all common
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.