ES6 create a class and add a total of methods

1.1 Creating classes

Syntax :

class name {
   //class body
}

Instantiation:

var xxx=new name()
//注意:必须使用new实例化对象

Example:

//创建一个class,此处创作一个 学校 类
class School {

}
//实例化这个类
new School()

1.2. Constructor class constructor

Definitions: constructor () method is the constructor function (default methods), it is to pass parameters, return an instance object generated instance object through the new command automatically when this method is called, if there is no display definition, within the class will automatically create a constructor ()
Example:
Next school class defined on a

//创建clas类
class School {
    constructor(sname){//sname参数名称
        this.sname=sname
    }
}
//实例化对象
var beida=new School('北京大学')
console.log(beida.sname)

Analysis: First, create a class, then the class constructor which has a constructor (), the function can receive parameters we pass, and be able to return back to Beida this example, you do not need to return to return, that is, when creating an instance when coupled with the new, it will automatically call the constructor () function and perform, then the 'Peking' passed to sname, then perform the next step sname assigned to this instance, sname, and this point is to create a beida, so this instance of the object beida have a sname this property.

class School {
   //类的共有属性放在constructor里面
    constructor(sname,special){
        this.sname=sname  //this指向创建的实列
        this.special=special
   }
 }
//创建实例
var beida=new School('北京大学','中文系')
var qinghua=new School('清华大学','英语系')
console.log(beida)
console.log(qinghua)

Analysis: The property has added a special, and see whether instantiate the object contains a property of the whole class.

Add 1.3 class method

grammar:

class name {
   constructor(){   //constructor构造器或者构造函数
       this.sname=sname
       this.speial=special
   }
   say(){
      console.log(this.sname+'我来了'
   }
}

Example:
Next add teach the method of example

class School {
   //类的共有属性放在constructor里面
    constructor(sname,special){
        this.sname=sname  
        this.special=special//this指向创建的实列
   }
   //创建一个teach方法
   teach(){
       console.log('我在学习')
   }
 }
//创建实例
var beida=new School('北京大学','中文系')
var qinghua=new School('清华大学','英语系')
console.log(beida)
console.log(qinghua)
beida.teach()//调用方法
qinghua.teach()

Syntax predetermined Note: (1) es6 class which all these functions do not need to function
(2) a plurality of method functions need to add a comma separated

Following the above example, a parameter of the write method

class School {
   //类的共有属性放在constructor里面
    constructor(sname,special){
        this.sname=sname  
        this.special=special//this指向创建的实列
   }
   //创建一个teach方法
   teach(){
       //console.log('我在学习')
       console.log('我在'+this.sname+'学习'+subject)
   }
 }
//创建实例
var beida=new School('北京大学','中文系')
var qinghua=new School('清华大学','英语系')
console.log(beida)
console.log(qinghua)
beida.teach('语文')//调用方法
qinghua.teach('英语')

After the subject receives the output parameter value of this method it is to teach 'I am learning the language of Peking University,' 'I studied English at Tsinghua University'.

Released two original articles · won praise 9 · views 74

Guess you like

Origin blog.csdn.net/weixin_46533954/article/details/105274298
Recommended