ES6新特性使用小结(五)
十二、class 与 extends
①、类的基本定义和生成实例
{
    class Parent{
        constructor(name='Lain'){          //定义构造函数
            this.name = name;
        }
    }
    let a = new Parent('Mayu');   //生成实例
    console.log(a);             //Parent {name: "Mayu"}
    //继承
    class Child extends Parent{         //通过 关键字 extends    实现继承
    }
    let b = new Child();
    console.log(b)      //Child {name: "Lain"}      继承了Parent 并使用了Parent的默认值
}
②、通过 extends 实现继承
{
    class Parent{
        constructor(name='Lain'){
            this.name = name;
        }
    }
    //继承
    class Child extends Parent{         //通过 关键字 extends    实现继承
        constructor(name='child'){      //定义了子类的默认值
            super(name);                //使用 super方法传递参数
            this.type='child';          //      **在继承关系中 如果使用了super 一定要将 super方法放在第一行
        }
    }
    let b = new Child();
    console.log(b)      //Child {name: "child", type: "child"}      继承了Parent 并使用了Child的默认值
}
③、class 中的 getter 和 setter
{
    class Parent{
        constructor(name='Lain'){
            this.name = name;
        }
        get longName(){         //  ** 这里是属性 而不是方法
            return  'Hello '+this.name;
        }
        set longName(value){
            this.name =value;
        }
    }
    let c = new Parent();
    console.log('getter',c.longName);   //getter    Hello Lain
    c.longName = 'abc';
    console.log('setter',c.longName);     //setter Hello abc
}
④、class 中的 静态方法
{
    class Parent{
        constructor(name='Lain'){
            this.name = name;
        }
        static tell(){          //  使用 关键字 static 定义静态方法
                                //  ***  该方法不会被实例继承,而是直接通过类来调用,这就称为“静态方法”。
            console.log('static');
        }
    }
    Parent.tell();      //static
}
⑤、class 中的 静态属性
{
    class Parent{
        constructor(name='Lain'){
            this.name = name;
        }
        static tell(){
            console.log('static');
        }
    }
    Parent.type  ='test';           // 在 class 定义完毕后 在 类 上直接定义 静态方法 而不是在实例上
    console.log('静态属性',Parent.type);        //静态属性 test
}
ES6新特性使用小结(五)的更多相关文章
- ES6新特性使用小结(三)
		
九.数据类型 Symbol /* * Symbol 数据类型 概念: Symbol 提供一个独一无二的值 * */ { let a1 = Symbol(); let a2 = Symbol(); co ...
 - ES6新特性使用小结(六)
		
十三.promise 异步编程 ①.使用 promise 模拟异步操作 { //ES5 中的 callback 解决 异步操作问题 let ajax = function (callback) { c ...
 - ES6新特性使用小结(四)
		
十一.Proxy .Reflect ①.Proxy 的概念和常用方法 { let obj = { //1.定义原始数据对象 对用户不可见 time: '2017-09-20', name: 'net' ...
 - ES6新特性使用小结(二)
		
六.Array 扩展 /* * Array Api Array.of 数组的构建 * */ { let arr = Array.of(, , , , , ); console.log(arr); // ...
 - ES6新特性使用小结(一)
		
一.let const 命令 'use strict'; /*function test(){ //let a = 1; for(let i=1;i<3;i++){ console.log(i) ...
 - ES6新特性概览
		
本文基于lukehoban/es6features ,同时参考了大量博客资料,具体见文末引用. ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代号harmony( ...
 - ES6新特性之模板字符串
		
ES6新特性概览 http://www.cnblogs.com/Wayou/p/es6_new_features.html 深入浅出ES6(四):模板字符串 http://www.infoq.c ...
 - Atitit js版本es5 es6新特性
		
Atitit js版本es5 es6新特性 Es5( es5 其实就是adobe action script的标准化)1 es6新特性1 Es5( es5 其实就是adobe action scrip ...
 - ES6新特性:Proxy代理器
		
ES6新特性:Proxy: 要使用的话, 直接在浏览器中执行即可, node和babel目前还没有Proxy的polyfill;,要使用的话,直接在浏览器中运行就好了, 浏览器的兼容性为:chrome ...
 
随机推荐
- ELK之方便的日志收集、搜索、展示工具
			
大家在做分部署系统开发的时候是不是经常因为查找日志而头疼,因为各服务器各应用都有自己日志,但比较分散,查找起来也比较麻烦,今天就给大家推荐一整套方便的工具ELK,ELK是Elastic公司开发的一整套 ...
 - html5--1.11列表
			
html5--1.11列表 学习要点: 无序列表 有序列表 列表的属性 自定义列表 1.无序列表的基本格式 ul(unorder line)标签里面放li标签就好了,每一项就是一个li(LineIte ...
 - codeforces 558A  A. Lala Land and Apple Trees(水题)
			
题目链接: A. Lala Land and Apple Trees time limit per test 1 second memory limit per test 256 megabytes ...
 - PyNLPIR python中文分词工具
			
官网:https://pynlpir.readthedocs.io/en/latest/ github:https://github.com/tsroten/pynlpir NLP ...
 - laravel登录后台500错误!
			
登录页面正常显示,填写完用户名密码 点登录后 页面一片空白,没有任何输出.debug可以看到一个500错误,preview和response都是空的.追踪了一下 发现在public/index.php ...
 - hdu3518 Boring Counting[后缀排序]
			
裸的统计不同的重复出现子串(不重叠)种数的题.多次使用后缀排序要注意小细节.y数组在重复使用时一定要清空,看那个line25 +k就明白了 ,cnt也要清空,为什么就不说了 #include<b ...
 - uC/OS-II源码分析(五)
			
每个任务被赋予不同的优先级等级,从0 级到最低优先级OS_LOWEST_PR1O,包括0 和 OS_LOWEST_PR1O 在内.当μC/OS-Ⅱ初始化的时候,最低优先级OS_LOWEST_PR1O ...
 - lwip 移植
			
一.源码目录结构 api . core.netif. include core下又有IPV4 . IPV6 . SNMP 和.c文件 include下又有IPV4.IPV6.LWIP.netif ne ...
 - Python手动安装 package
			
https://pypi.python.org/pypi 下载 解压 进入setup.py的目录 python setup.py build python setup.py install
 - Hash表的实现
			
#include "stdafx.h" #include <iostream> #include <exception> using namespace s ...