javaScript 基础知识汇总 (十三)
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 指向了 A:B.[[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 基础知识汇总 (十三)的更多相关文章
- JavaScript基础知识汇总
1. 图片热区: <img src="logo.jpg" usemap="#logo"> <map id="logo" n ...
- javaScript 基础知识汇总(三)
1.循环:while 和 for while 循环 while(condition){ //代码 循环体 } do ... while 循环 let i =0; do { //循环体 }while( ...
- javaScript 基础知识汇总(六)
1.基本类型与对象的区别 基本类型:是原始类型的中的一种值. 在JavaScript中有6中基本类型:string number boolean symbol null undefined 对 ...
- javaScript 基础知识汇总(五)
1.垃圾回收 JavaScript 的内存管理是自动的,不能强制执行或者阻止执行 可达性 JavaScript中主要的内存管理概念是可达性. 什么是可达性? 定义一个对象 let user = { n ...
- javaScript 基础知识汇总(二)
1.运算符 术语或者叫法:一元运算符.二元运算符.运算元(参数) let x=0; x=5+2; //5和2为运算元,“+” 为二元运算符: x=-x; //"-" 为一元运算符 ...
- javascript 基础知识汇总(一)
1.<script> 标签 1) 可以通过<script> 标签将javaScript 代码添加到页面中 (type 和language 属性不是必须的) 2)外部的脚本可以通 ...
- JavaScript 基础知识汇总目录
一.标签.代码结构.现代模式.变量.数据类型.类型转换 GO 二.运算符.值的比较.交互.条件运算符.逻辑运算符 GO 三.循环 while 和 for .switch语句.函数.函数表达式和箭头函数 ...
- javaScript 基础知识汇总(七)
1.数组 特点:数组是可以存储有序集合的对象. 声明: let arr = new Array(); let arr=[]; 大多数情况下我们使用第二种. let fruits = [" ...
- javaScript 基础知识汇总 (十)
1.New Function 语法:let func = new Function ([arg1[, arg2[, ...argN]],] functionBody) //无参数示例: let say ...
随机推荐
- async包 ES6 async/await的区别
最基本的async 包 ApCollection.find({}).toArray(function (err, aps) { var num = 0; async.whilst( function ...
- RNA sequence单分子直测技术
生命组学 按照功能分类遗传物质,可能的分类有系统流.操作流.平衡流等等.下面是使用该理论解释DNA与RNA的关系: DNA和RNA有很大不同,DNA存储遗传信息,作为生命活动的最内核物质,如同操作系统 ...
- php--0与空的判断
使用empty()函数判断,两者都是true $a=0; if(trim($a)=="") { echo '数字0'; }
- python django ORM
1.在models.py中创创建类 # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db imp ...
- UnitTest测试框架-操作步骤
一.UnitTest 1. TestCase 说明:测试用例 1.新建类并集成unittest.TestCase 2. TestSuite 说明:测试套件(多条用例) 方法: 1. 实例化 suite ...
- hql错误:No data type for node: org.hibernate.hql.ast.tree.IdentNode
今天写了一个查询,用的是hql,数据库是mysql.多表联查,结果报错了报: \-[IDENT] IdentNode: 'routerNumber' {originalText=routerNumbe ...
- Eclipse添加comment
1.给新增的Java类,自动生成Comment. 打开Preferences,找到Java-->Code Style -->Code Templates-->Comments--&g ...
- 使用Commons Logging
Commons Logging 和Java标准库提供的日志不同,Commons Logging是一个第三方日志库,它是由Apache创建的日志模块,需要导入commons-logging-1.2.ja ...
- javascript学习内容
http协议 犀牛书 MDN js单线程 let只在代码块内有效 es5只有全局作用域 const变量指向的内存地址不得改动,值不能保证不变 全局变量不加var node.js 更改连接到服务器的方式 ...
- Windows系统在Python2.7环境下安装numpy, matplotlib, scipy - Lichanghao Blog
numpy, matplotlib, scipy三个包是科学计算和绘图的利器.安装它们既可以在网上下载exe安装包,也可以用python内置的包管理工具来下载安装,后者较为方便. 这几天做美赛要用到, ...