1.定义
我们使用 class 关键字来创建一个类,类体在一对大括号 {} 中,我们可以在大括号 {} 中定义类成员的位置,如方法或构造函数。
每个类中包含了一个特殊的方法 constructor(),它是类的构造函数,这种方法用于创建和初始化一个由 class 创建的对象。
class ClassName{
constructor(){...}
}
js没有类 是原型的思想设计的类,但是学习和使用这个技术时,心中要用类的思想学习
class Person{
constructor(a,b,c){
console.log(666666)
//不写构造函数默认有一个空的
this.x=a
this.b=b
}
// 对象的属性
life=50
//对象的原型对象的方法
say1(){
console.log(111,this.x)
}
//类的静态属性
static life=200
//类的静态方法
static say2(){
console.log(222)
}
}
继承
//“父类”
class Person {
life = 1
constructor(name) {
this.name = name
this.makeMoney = function() {
console.log(this.name + "赚钱")
}
}
run() {
console.log(this.name + "跑步")
}
static URL = "www"
}
子类
class Student extends Person {
constructor(name) {
super(name) //让Person类的构造函数的逻辑去帮我们构建对象空间
this.age = 20
this.book = "书名"
}
life=1
constructor(name){
this.name=name
this.makeMoney=function(){
console.log(this.name+"赚钱")
}
}
run(){
console.log(this.name+"跑步")
}
}
var s1 = new Student("marry")
这时候子类打出来就既有继承下来的元素也有子类的元素