区别于es5,es6中使用class类创建对象实例,更加简洁
<script>
// es5通过Function创建构造函数,通过构造函数创建对象(关键字new)
// es6通过class类创建一个类,类里面的方法默认是在原型prototype上,默认的constructor是构造函数
class Stu{
// 在类中可直接书写方法,无需function关键字
// 在class中直接书写的函数,其实就是写在原型中
study(){
console.log('code');
}
// 在class中默认有一个constructor函数,相当于es5中的构造函数,用于写属性
constructor(name,age){
this.name = name
this.age = age
}
}
let stu1 = new Stu('kelly',18)
let stu2 = new Stu('kk',25)
console.log(stu1);
console.log(stu2);
console.log(stu1.study == stu2.study); // true
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。