Introduction JavaScript in class Class

The concept of class

First, why should we class to class?

Because the object is to create a class by class, so that developers do not have to write duplicate code to achieve the purpose of code reuse. It is based on the logic is similar to the structure and function of the two or more objects, a template can be abstracted,

Copy a number of similar objects based on a template. Over and over again just like car manufacturers reuse the same template to create a large number of automotive vehicles, we defer the class understood as a template.

But why we do not function function is used to re-use it?

Because funcion declaration is needed to enhance the state, rather than class, class who needs to re-use.

Two, class class syntax

 1     class Student{
 2         constructor(name,age,sex){
 3             this.name = name
 4             this.age= age
 5             this.sex = sex
 6         }
 7 
 8         read(){console.log(this.name+this.age+this.sex)}
 9     }
10     var Tom =new Student('tom',21,'男')
11     Tom.read()

Tom by Student class object instantiated out. Tom is in accordance with the Student objects this template instantiation out of the object. Examples of pre-customized out of the objects have a good structure and function.

2.1 constructor constructor

constructor method is a special method to create and initialize an object. In a class you can have only one special method named constructor, if it contains more will be given.

super keyword in the constructor by calling the parent class constructor method.

 

2.2 static (static method)

By the static keyword to create a class static method

. 1  class Student {
 2      // static property 
. 3      static P = 2 ;
 . 4      // constructor 
. 5      constructor (name, Age, Sex) {
 . 6          the this .name = name
 . 7          the this .age = Age
 . 8          the this .sex = Sex
 . 9      }
 10      // example method      dream () {console.log ( 'salary over a million' )}
 12  }
 13  // static property call 
14 the console.log (student.p)

2.3 class inheritance extends

 1 class A {
 2      constructor(){
 3             this.name = 'Marry'
 4             this.age= 18
 5         }
 6     read(){console.log(this.name+this.age)}
 7 }
 8 class B extends A {
 9     constructor(props) {
10         super(props)     
11     }
12     s(){
13         console.log(this.name+this.age)
14     }
15 }
16 var b = new B();
17 b.s()

When the call instance b s method, b does not have the name and age, you will find A The inheritance

2.4 "this" points to the problem

this refers to the class object instance.

2.5 super () keyword

Call the parent class method appropriate for the keyword super, which is an advantage compared to the prototype inheritance

III. The overall wording

1  // Create a stored class-specific methods and properties for the object instance. 
2  class Student {
 . 3      // Static attributes belonging only Student properties 
. 4      static S = "180 [" ;
 . 5      // static method belongs Student methods 
. 6      static m ({)
 . 7          the console.log ( "static method" )
 8      }
 9      // constructor 
10      constructor (The props) {
 . 11          // instance attribute 
12 is          the this .name = props.name;
 13 is          the this .age = props.age;
 14          the this .sex = props.sex;
 15      }
16      @ Examples of the method 
. 17      Students. () {
 18 is          the console.log ( the this .name + the this .age + the this .sex)
 . 19  
20 is      }
 21 is  }
 22 is  // static property called 
23 is  the console.log (Student.s)
 24  // instantiate Object 
25  var Sl = new new Student ( 'Tom', 21 is, 'M' );
 26  // call the method 
27 S1.students ()

More than purely personal understanding, if in doubt welcome message

Guess you like

Origin www.cnblogs.com/z-j-c/p/12063559.html