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 ...
随机推荐
- BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673 题意: 有n个砝码(n <= 1000),重量为w[i]. 你要从中选择一些砝 ...
- lucene内置的评分函数
For multiterm queries, Lucene takes the Boolean model, TF/IDF, and the vector space model and combin ...
- 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 ...
- AtCoder Beginner Contest 106 2018/08/18
A - Garden Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement There is a ...
- iOS UINavgationController、 UINavigationBar、 UINavigationItem关系分析
一般导航控制器含有4个对象,UINavigationController.UINavigationBar.UIViewController.UINavigationItem. 1:UINavigati ...
- bzoj 2067 [Poi2004]SZN——二分+贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2067 最少的线段可以贪心地想出来.(结果还是写错了)就是偶数孩子可以自己配对,奇数孩子要带一 ...
- poj3784Running Median——堆维护中间值
题目:http://poj.org/problem?id=3784 将较小的数放入大根堆,较大的数放入小根堆,控制较小数堆大小比较大数堆小1,则较大数堆堆顶即为中位数. 代码如下: #include& ...
- 如何让Surface RT支持网站的flash
Go to the desktop version of IE10, hit the click ALT button on your keyboard, click on and then Comp ...
- POJ1511(最短路大数据处理)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 23357 Accepted: 767 ...
- .NET 下的 POP3 编程代码共享
前一段时间在论坛上看见有人问如何使用.net进行POP3编程,其实POP3的使用很简单,所以.net没有向SMTP那样给出相应的类来控制. 废话少说,程序员最需要的使代码. 1.打开VS.NET 20 ...