class中constructor()方法: https://blog.youkuaiyun.com/boyikenan123/article/details/124609533
class Student {
constructor(sex,age) {
// 在这里, 它调用了父类的构造函数, 提供给 People 的"sex"和"age"
//super(sex, age);// 注意: 在派生类中(如果是继承的), 必须先调用 super() 才能使用 "this"。
// 忽略这个,将会导致一个引用错误。
this.name = 'xiao hua';
this.age=age//如果要对传入的数据进行操作
this._num=null
this.addChildFn=null
}
//外面实际用到比如.nameAge才会进这些方法
get nameAge(){
return `名字是:${this.name}、年龄是${this.age}`
}}
//赋值过程
set num(v){
this._num=v
}
//取值过程
get num(){
return this._num+5
}
addChild(){
if(this.addChildFn){
this.addChildFn()
}
}
};
/////////////////////////////////////////////////////////////////////
//vue页面,引入后使用
let student=new Student('男','10')
consolo.log(student.nameAge)//名字是:xiao hua、年龄是10" 会进入get nameAge()
conosle.log(node.num=5)//5 进入set num() 返回5
console.log(node.num)//10 进入get num() 返回10
//bind
student.addChildFn=this.addChildFn.bind(this)
static
class Car extends Student {
constructor(name) {
this.name = name;
}
static hello(name) {
return "Hello " + name;
}
hello2(name) {
return "Hello " + name;
}
}
let myCar = new Car("Ford");
//hello是static 方法,只能在类本身上调用该方法
document.getElementById("demo").innerHTML = Car.hello('李华');
//hello2就可以直接用实例上的方法
document.getElementById("demo").innerHTML = myCar .hello2('李华');