1、Class

  在JavaScript中 calss即类是一种函数

  基本语法

  class Myclass{

    constructor(){}

    method1(){}

    method2(){}

    method3(){}

  }

  示例:

  class User{

    constructor(name){//构造函数

      this.name = name;

    }

    sayHI(){//属性方法

      alert(name);

    }

  }

  //使用方法

  let user = new User("XiaoMing");

  user.sayHi();

  类表达式

  let User = class{

    sayHi(){

      alert('Hello');

    }

  };

2、类继承

  在JavaScript中类继承通过 extends关键字实现。示例:

 class Animal {
constructor(name) {
this.speed = ;
this.name = name;
}
run(speed) {
this.speed += speed;
alert(`${this.name} runs with speed ${this.speed}.`);
}
stop() {
this.speed = ;
alert(`${this.name} stopped.`);
}
} // 通过指定“extends Animal”让 Rabbit 继承自 Animal
class Rabbit extends Animal {
hide() {
alert(`${this.name} hides!`);
}
} let rabbit = new Rabbit("White Rabbit"); rabbit.run(); // White Rabbit runs with speed 5.
rabbit.hide(); // White Rabbit hides!

  重写方法

  直接重新定义,从新写相同名字的就可以

 class Rabbit extends Animal {
stop() {
// ...这将用于 rabbit.stop()
}
}

  如果想在字类中仍使用父类的方法,又不直接重写,则可以 通过 super 来实现

  

 class Animal {

   constructor(name) {
this.speed = ;
this.name = name;
} run(speed) {
this.speed += speed;
alert(`${this.name} runs with speed ${this.speed}.`);
} stop() {
this.speed = ;
alert(`${this.name} stopped.`);
} } class Rabbit extends Animal {
hide() {
alert(`${this.name} hides!`);
} stop() {
super.stop(); // 调用父类的 stop 函数
this.hide(); // 然后隐藏
}
} let rabbit = new Rabbit("White Rabbit"); rabbit.run(); // White Rabbit runs with speed 5.
rabbit.stop(); // White Rabbit stopped. White rabbit hides!

  重写构造函数

  不能直接重新写,要先执行super 之后才可以

  原因:1) 当一个普通构造函数执行时,它会创建一个空对象作为this,并继续执行

     2)但是当继承的构造函数执行时,它并不会做这件事情,而是运行符类中的构造函数来完成这项工作。

  用法示例;

  class Animal{

    constructor(name){

      this.speed = 0;

      this.name = name;

    }

  }

  class Rabbit extends Animal{

    constructor(name,earLength){

      super(name);

      this.earLength = earLength;

    }

   }

  let rabbit = new Rabbit("White Rabbit",10)

  alert(rabbit.name);

  alert(rabbit.earLength);

  箭头函数没有自己的this或super,所以它们能融入到就近的上下文。

3、静态属性和方法

  通过关键字static 设置静态方法或者静态属性

  静态方法:

  class User{

    static staticMethod(){

      alert(this === user);

    }

  }

  User.staticMethod();//true

  静态属性:

  class Article{

    static name = "xiaoming";

  }

  alert(Article.name);

  静态属性和方法是被继承的

  对于 class B extends A,类 B 的 prototype 指向了 AB.[[Prototype]] = A。因此,如果一个字段在 B 中没有找到,会继续在 A 中查找。  

4、私有的和受保护的属性和方法

  受保护的属性通常以下划线_作为前缀

  class CoffeeMachine{

    _waterAmount = 0;

    set waterAmount(value){

      if(value<0) throw new error("Negative water");

      this._waterAmount = value;

    }

    get waterAmount(){

      return this._waterAmount;

    }

  }

  let cofMachine = new CoffeeMachine();

  cofMachine.waterAmount = 10;

  alert(cofMachine.waterAmount);//10

  设置只读

  只有get 方法没有set 方法

  初始化可以给默认值或者通过构造函数赋值

  class CoffeeMachine{

    constructor(power){

      this._power = power;

    }

    get power(){

      return this._power;

    }

  }

5、类型监测 “instanceof”

  用法:obj instanceof Class

   如果 obj隶属于Class 或者是Class 衍生的类,表达式返沪true

  类型监测图表

  

javaScript 基础知识汇总 (十三)的更多相关文章

  1. JavaScript基础知识汇总

    1. 图片热区: <img src="logo.jpg" usemap="#logo"> <map id="logo" n ...

  2. javaScript 基础知识汇总(三)

    1.循环:while 和 for while 循环 while(condition){ //代码 循环体 } do ... while  循环 let i =0; do { //循环体 }while( ...

  3. javaScript 基础知识汇总(六)

    1.基本类型与对象的区别 基本类型:是原始类型的中的一种值. 在JavaScript中有6中基本类型:string number  boolean  symbol  null  undefined 对 ...

  4. javaScript 基础知识汇总(五)

    1.垃圾回收 JavaScript 的内存管理是自动的,不能强制执行或者阻止执行 可达性 JavaScript中主要的内存管理概念是可达性. 什么是可达性? 定义一个对象 let user = { n ...

  5. javaScript 基础知识汇总(二)

    1.运算符 术语或者叫法:一元运算符.二元运算符.运算元(参数) let x=0; x=5+2; //5和2为运算元,“+” 为二元运算符: x=-x; //"-" 为一元运算符 ...

  6. javascript 基础知识汇总(一)

    1.<script> 标签 1) 可以通过<script> 标签将javaScript 代码添加到页面中 (type 和language 属性不是必须的) 2)外部的脚本可以通 ...

  7. JavaScript 基础知识汇总目录

    一.标签.代码结构.现代模式.变量.数据类型.类型转换 GO 二.运算符.值的比较.交互.条件运算符.逻辑运算符 GO 三.循环 while 和 for .switch语句.函数.函数表达式和箭头函数 ...

  8. javaScript 基础知识汇总(七)

    1.数组 特点:数组是可以存储有序集合的对象. 声明: let arr = new Array();   let arr=[]; 大多数情况下我们使用第二种. let fruits = [" ...

  9. javaScript 基础知识汇总 (十)

    1.New Function 语法:let func = new Function ([arg1[, arg2[, ...argN]],] functionBody) //无参数示例: let say ...

随机推荐

  1. CSA|EI

    信息检索 CSA是学科特色的包含相关学科的内容,其网址是https://search.proquest.com/ 可以使用命令行检索: 分类的限制检索: 寻找检索线索可使用百科全书 EI是工程领域最全 ...

  2. 经典题型-打印小星星(python)

    # * # * * # * * * # * * * * # * * * * * x = 0 while x < 5: x += 1 # 每次循环需要给y赋值0.清空y中存储的值 y = 0 wh ...

  3. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  4. yii2.0 集合七牛SDK 上传图片到第三方

    首先,请用composer下载七牛phpSDK (具体参考官方文档) composer require qiniu/php-sdk 注册七牛账号 获取 AK SK(密匙) ,创建资源对象 获取doma ...

  5. 吴裕雄--天生自然 R语言开发学习:分类(续一)

    #-----------------------------------------------------------------------------# # R in Action (2nd e ...

  6. mysql视图、事务、触发器、索引

    视图 什么是视图 ? 一个查询语句的结果是一张虚拟表,将这种虚拟表保存下来,它就变成了一个视图. 为什么要用视图? 当频繁需要用到多张表的连表结果,你就可以事先生成好视图,之后直接调用即可,避免了反复 ...

  7. 阿里投资Magic Leap 是美酒还是毒药?

    Leap 是美酒还是毒药?" title="阿里投资Magic Leap 是美酒还是毒药?"> 土豪阿里又摊上"大事"了!但这次不是让人头痛的假 ...

  8. 通俗易懂DenseNet

    目录 写在前面 Dense Block与Transition Layer DenseNet网络架构与性能 理解DenseNet Plain Net.ResNet与DenseNet 参考 博客:博客园 ...

  9. Liferay7 Intellij IDEA 开发环境搭建

    一.安装Liferay插件 安装过程不在赘述,推荐两种安装方式: 通过Intellij插件市场安装 通过下载插件zip包安装 安装完成后,在项目板块中点鼠标右键,会出现Liferay菜单. 二.安装L ...

  10. 【算法记事本#NLP-1】最大匹配算法分词

    本文地址:https://www.cnblogs.com/oberon-zjt0806/p/12409536.html #NLP-1 最大匹配算法(MM) 最大匹配算法(Maximum Matchin ...