ES6---通过class构造函数extends、public在其中的作用
来源:互联网 发布:淘宝网怎么加好友呢 编辑:程序博客网 时间:2024/06/05 18:53
在es5中,构造函数生成的对象实例一般包括私有属性和共用方法,其中共用方法一般是通过构造函数的原型进行定义。而es6中,通过class类进行定义构造函数则不用借助于原型….
●class定义类:
/////////class people{ constructor(name){this.name=name} sayName(){return 'I am '+this.name}}//constructor:构造方法,this指向对象实例,class类构造方法必须要有constctor方法;sayName:people类的方法,不需要加function,class类中的方法是不可枚举的,存在于原型中let per1=new people();//空let per2=new people('Ming');//people {name: "Ming"}per2.sayName()//"I am Ming"per2.__proto__.hasOwnProperty('sayName');//trueper2.__proto__.ccc=function(){return 'ccc'};per1.ccc();//ccc,per1和per2的__proto__指针指向相同的原型,故为per2原型增加ccc方法时,per1也同样可以调用
●class中的继承: es5中可以通过原型、call等实现继承,es6中class类可以使用extends实现继承
class people1{};class people2 extends people1{ constructor(name){ this.name=name; }}let per=new people2('M');//报错,没有super关键字class people1{constructor(x){this.x=x}};class people2 extends people1{ constructor(x,name){ super(x); this.name=name; }}let per=new people2('a','M');//people2 {x: "a", name: "M"},super()方法必须使用,其用来调用父类的constructor方法,其必须放于首位。
●getter和setter:class people{ constructor(){} get prop(){return 'i am getter'} set prop(v){return 'i am '+v}}let per=new people();per.prop='abc';//i am abc 设置属性per.prop;//i am getter 读取属性
1 0
- ES6---通过class构造函数extends、public在其中的作用
- public class Jbpm_ActorGoOn extends TestCase
- public class DlgShowTable extends JDialog
- public class TableBcpProcessor extends Thread
- public class LoginAction extends ActionSupport {
- C# class 前加 Public 的作用
- ES6学习记录之class, extends, super和箭头函数与this
- 通过 Class.forName实例化带来参数的构造函数
- 构造函数的执行过程与构造函数的规则,instanceof在构造函数中的作用
- 构造函数的作用
- 构造函数的作用
- 构造函数的作用
- 构造函数的作用.
- 构造函数的作用
- ES6 类(Class)的继承(extends)和自定义存(setter)取值(getter)详解
- ES6-正则的扩展-RegExp构造函数
- ES6-extends
- ES6中箭头函数的作用
- C++抽象编程——STL(6)——map类的简单使用
- linux根目录下各文件夹的作用
- 关于oracle sql developer
- Android中的三级缓存解析与实战
- JAVA经典算法,每日一题
- ES6---通过class构造函数extends、public在其中的作用
- 一步一步写web之react实战(三-新建项目与代码分析)
- 机器学习之——绪论部分
- 进程和线程
- 深度图像的获取原理
- struts2学习笔记
- 编程内容:30个随机四则运算
- CodeForces-221B-Little Elephant and Numbers [枚举]
- Debug下正常运行,但调成Release时遇到三个问题及其完美解决!