js高级

74 阅读1分钟

webAPI第十天

面向对象

对象特征

1 属性

2 方法

关键字

构造函数 Person

function Person(name){ this.name=name; }

实例 p1

const p1 = new Person("悟空")

原型对象 Person.prototyp

在原型对象上添加的方法 可以被实例共用

继承

属性继承 call

function Student(name){ Person.call(this,name) }

方法继承 prototype

Student.prototype.say = Person.prototype.say

image.png

借调 call

可以调用别人的函数

后面补充 call bind apply

实际案例 和 dom结合相关

es6

es6的class 的出现 基本上可以替代了es5的构造函数和原型,使之代码结构上更加简洁。

关键字

  1. class
  2. 属性
  3. 方法
  4. 继承 extends
  5. 构造函数 constructor
  6. 方法重写 override:子类方法覆盖父类,super.父类方法()
  7. 父类的构造函数 super :子类有构造方法且使用this前,必须使用super()

函数参数默认值

function func(a=123){}

a=123 设置默认值

对象的简写

const name="悟空";

const obj = { name }

obj.name = "悟空"

const ob ={ say(){} }

解构

数组

const [a,b] =[100,200]

a = 100, b =200

对象

const {name,height} = { name:"悟空",height:100}

name = "悟空"

height = 100

展开-拓展运算符-剩余运算符

剩余 ...

const [a,...b]=[1,2,3,4]

b = [2,3,4]

const {a,...b}={a:1,b:2,c:3}

b={b:2,c:3}

function calc(...args){

}

args = 数组

args 存放传入calc方法所有的参数

展开

const arr =[1,2,3];

const newArr=[...arr,4]

newArr=[1,2,3,4]

const obj={name:悟空"}

const newObj={...obj,color:"red"}

newObj={name:"悟空",color:"red"}

数组去重

仔细讲解了方法

for、some、filter、for+splice

开阔视野

indexOf,findIndex,find,includes,set

image.png

image.png