在構造函數對象上面加一個方法函數
class Student {
constructor(name, age) {
this.name = name ;
this.age = age;
this.sayName = this.sayName
},
sayName() {
console.log(this.name)
},
static sayHello() {
console.log('helloworld')
}
}this
function Student(name, age) {
this.name = name ;
this.age = age ;prototype
this.sayName = function() { console.log(this.name) }
}code
Student.prototype.sayName = function() {
console.log(this.name)
}對象
Student.sayHello = function() {
console.log('helloworld')
}blog