ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ES6中的class 与prototype

2021-06-12 15:58:35  阅读:186  来源: 互联网

标签:ES6 name age Person constructor prototype class


一、定义构造函数  

在以前的js中,生成一个对象实例,需要先定义构造函数,然后通过prototype 的方式来添加方法,在生成实例:

      

复制代码

function Person(){
       this.name = "测试";
       this.age = 26;
}

 Person.prototype.getName = function(){
             console.log("name:" + this.name)

}

var  p = new Person()

复制代码

 

然而系现在的ES6

复制代码

class Person{
     constructor(name, age){
                this.name = name;
                this.age = age;
           }
     getName() {
                return this.name;
           }
}

var p = new Person("luoqiang",26)

复制代码

在ES5中原本的构造函数被constructor 替代,本来需要定义在prototype上面的,方法直接定义在class里面即可。

ES6中的类的数据类型就是函数,类本身指向构造函数,使用的时候也需要new命令。

类中所有的方法都定义在类的prototype属性上面。

 

class B {}
let b = new B();

b.constructor === B.prototype.constructor // true

 

二、Class 的静态方法

   ES6 中类有静态方法,即一个方法前加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用。

        

复制代码

class Food {
static classMethod() {
   return 'hello'
}
}

Food.classMethod() // "hello"

var poo = new Food();
poo.classMethod() // TypeError: poo.classMethod is not a function

复制代码

  通过类直接调用,输出的是hello,实例化以后不能调用。

  PS:

    在react、 RN中,this.state ={} 这种写法是在constructor 里面定义实例属性。

    

复制代码

class ReactCounter extends React.Component {
  state;
  constructor(props){
    super(props);
     this.state = {
        count:0
  }
 }
}

复制代码

    super(props)是继承父类的props 属性,state是在子类中定义实例属性。

 

三、class 继承

以前的继承方式:

复制代码

function Person(name,age){
           this.name = name;
           this.age = age;
}
 Person.prototype.getName = function(){
         console.log("name:" + this.name);
}
 
 function Stu(stu_class,name,age){
         Person.call(this,name,age);
         this.stu_class=stu_class;
}
 
 Stu.prototype=new Person;
 Stu.prototype.constructors=Stu;
 Stu.prototype.getClass=function(){
        console.log("班级:" + this.stu_class)
}

// 得到一个学员信息对象

var s= new Stu()
console.log(s)

复制代码

ES6的继承:

 

复制代码

   class Person{
     constructor(name, age){
               this.name = name;
               this.age = age;
        }
       getName(){
                   return this.name;
        }
}

class Student extends Person{
       constructor(stu_class,name,age){
         //需注意如果一个子类继承父类,必须调用super,才能使用constructor,使用this
               super(name,age)

       }
     getClass(){
        
		console.log("班级:"+this.stu_class)
      }

}


var p=new Person("luoqiang",26)
console.log(p)

复制代码

 

注意点:

Class 定义方法是不能使用箭头函数

Class 也可以使用表达式方式声明

标签:ES6,name,age,Person,constructor,prototype,class
来源: https://blog.csdn.net/zhousenshan/article/details/117848859

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有