- 使用原型
function cat(name) {
this.name = name
}
cat.prototype.type = 'animal'
cat.prototype.run = function () {
console.log(`${this.name} is running`);
}
let cat1 = new cat('猫咪')
cat1.run()
- 使用类
class cat{
constructor(name) {
this.name=name
}
run() {
console.log(`${this.name} is running`);
}
}
let cat1 = new cat('猫咪')
cat1.run()