class Animal {
// 类的构造器constructor
constructor() {
this.a = 10;
this.b = 'xxx';
}
// 类的方法
getA() {
console.log(this.a)
}
getB() {
console.log(this.b)
}
}
const animal1 = new Animal();
console.log(animal1)
function Animal2() {
// 类的构造器就是方法内的内容
this.a = 10;
this.b = 'xxx';
}
// 类的方法就是方法原型上的方法
Animal2.prototype.getA = function () {
console.log(this.a);
}
Animal2.prototype.getB = function () {
console.log(this.b);
}
const animal2 = new Animal2();
console.log(animal2)
// 实例animal2的__proto__ 指向构造函数Animal2的原型prototype
console.log(animal2.__proto__ === Animal2.prototype);
class和构造函数的区别
最新推荐文章于 2023-02-16 12:17:25 发布