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 ...
随机推荐
- scarce|component|
ADJ-GRADED 缺乏的;不足的;供不应求的If something is scarce, there is not enough of it. Food was scarce and expen ...
- cisco WLC开启portal认证,但是访问https无法跳转问题的解决
config network web-auth https-redirect enable版本8,及以上才支持 官方文档: http://www.cisco.com/c/zh_cn/support ...
- PHP PSR-2 代码风格规范
代码风格规范 本篇规范是 PSR-1 基本代码规范的继承与扩展. 本规范希望通过制定一系列规范化PHP代码的规则,以减少在浏览不同作者的代码时,因代码风格的不同而造成不便. 当多名程序员在多个项目中合 ...
- Luogu_2434_[SDOI2005]区间
题目描述 现给定n个闭区间[ai, bi],1<=i<=n.这些区间的并可以表示为一些不相交的闭区间的并.你的任务就是在这些表示方式中找出包含最少区间的方案.你的输出应该按照区间的升序排列 ...
- Leetcode刷题记录 剑指offer
面试题3:数组中重复数字 # 使用set,时间复杂度O(n),空间复杂度O(n)class Solution(object): def findRepeatNumber(self, nums): &q ...
- Python学习笔记(四)函数式编程
高阶函数(Higher-order function) Input: 1 abs Output: 1 <function abs> Input: 1 abs(-10) Output: 1 ...
- 彪悍的Surface Book2发布:能重拾笔记本行业的信心吗?
Book2发布:能重拾笔记本行业的信心吗?" title="彪悍的Surface Book2发布:能重拾笔记本行业的信心吗?"> 在智能手机全面普及之后, ...
- Android APP性能及专项测试(个人整理)
移动测试. Android测试 .APP测试 Android篇 1. 性能测试 Android性能测试分为两类:1.一类为rom版本(系统)的性能测试2.一类为应用app的性能测试 Android ...
- O2O倒逼汽车行业改革:服务超越销售成重心
汽车作为越来越普及的出行工具,已经进入千家万户.与其他家具.家电.服饰等产品不同,从新车销售到售后保养.保险购买.维修乃至报废阶段,都能细分化,形成一个个潜力巨大的垂直领域.而随着互联网元素的渗入,汽 ...
- 作为前端,你需要懂得javascript实现继承的方法
在ES6之前,javascript不跟其他语言一样,有直接继承的方法,它需要借助于构造函数+原型对象模拟实现继承.现在我们可以利用ES6的extends方法实现继承,如果想了解更多有关ES6实现的继承 ...