es5
function P1(x,y){
this.x = x;
this.y= y;
}
P1.prototype.run = function(){
console.log('run跑起来,x',this.x);
}
let p1 = new P1(99,88);
p1.run();
P1.xx = '挂载到函数上';
console.log('p1.xx',p1.xx)
es6
class P2{
static xx = '挂载到类上';
//构造函数中只做一件事:初始化数据
constructor(x,y){
this.x = x;
this.y= y;
}
run(){
console.log('run跑起来,x',this.x);
}
}
let p2 = new P2(99,88);
p2.run();
console.log('p2.xx',p2.xx);
继承
class A {
constructor(a){
console.log('父类构造函数执行了');
this.a = a;
}
}
//继承只能单继承
class B extends A {
constructor(c,d){
//需要执行父类构造函数:让我们具备a属性
super(d);
this.b = c;
console.log('b中的this',this);
}
}
//如果出现同名属性:优先子类
let b1 = new B('c','d');
console.log(b1);
let a1 = new A('f');
console.log(a1);