13、ES6 之class
来源:互联网 发布:农村淘宝发展现状分析 编辑:程序博客网 时间:2024/04/20 10:28
1、 定义构造函数的语法 class + 自定义的名字 {}
function fn(){
}
// 上面的 等于 下面
class Cat{
// 构造函数本身
constructor(){
}
}
2. 如果是静态的方法 不用new 可以直接调用
class Cat{
// 构造函数本身
constructor(name){
this.name = name;
}
getname(){
return this.name
}
// 如果是静态的方法 不用new 可以直接调用
static getcolor(color){
console.log(color);
}
}
Cat.getcolor(`#fff`);// 如果是静态的方法 不用new 可以直接调用
运行结果:
3、extends 继承
class Cat{
// 构造函数本身
constructor(name){
this.name = name;
}
getname(){
return this.name
}
// 如果是静态的方法 不用new 可以直接调用
static getcolor(color){
console.log(color);
}
}
class Dog extends Cat{
constructor(name){
super(name);// super 继承父类本身的constructor里面的方法
}
static getcolor(color){
super.getcolor(color);
}
}
let d = new Dog(`狗`);
console.log(d.getname());
运行结果:
4、调用父类的方法 super.方法名//如果父类的是静态的在子类也只能在静态处调用,否则会报错
class Cat{
// 构造函数本身
constructor(name){
this.name = name;
}
getname(){
return this.name
}
// 如果是静态的方法 不用new 可以直接调用
static getcolor(color){
console.log(color);
}
}
class Dog extends Cat{
constructor(name){
super(name);// super 继承父类本身的constructor里面的方法
}
static getcolor(color){
super.getcolor(color);//调用父类的方法
}
}
Dog.getcolor(`#ccc`);
用法详细说明:
http://es6.ruanyifeng.com/#docs/class
- 13、ES6 之class
- javascript ES6之class
- ES6学习笔记之class
- 初步探究ES6之class类
- ES6基础之Class基本语法
- ES6基础之Class的继承
- ES6之Class的常用功能解读
- ES6--Class
- ES6 Class
- ES6--class
- ES6-class
- 【ES6】class
- 13、ES6 之async
- ES6详解三:class
- ES6详解三:class
- ES6 Class 类
- 自学-ES6篇-Class
- es6中class学习
- Android SwipeRefreshLayout+ViewPager+ScrollView 滑动冲突解决
- jquery编写的轮播图
- Java基础知识思维导图
- TextView 跑马灯效果无效果解决方案(stackoverflow)
- webscoket 的定义
- 13、ES6 之class
- Navicat Premium 11.2.7 中文破解版安装
- 准备
- 如何解决ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- Windows 下如何查看文件夹被哪个进程所占用
- linux查询已安装的包 RPM
- Linux设备驱动模型
- thinkphp 模板中使用mb_substr(),substr()函数
- SpringMVC解决跨域问题,以及跨服务器调用时候session丢失的问题总结!!!