es5:
let Person = function (type) {
this.type = type
}
Person.prototype.speak = function () {
console.log('hhhhhh')
}
let student = new Person('student')
let teacher = new Person('teacher')
student.speak = function () {
console.log('I love studying!')
}
student.speak() // I love studying!
teacher.speak() // hhhhhh
es6:
class Person {
constructor (type) {
this.type = type
}
speak () {
console.log('hhhhhh')
}
}
let student = new Person('student')
let teacher = new Person('teacher')
student.speak = function () {
console.log('I love studying!')
}
student.speak() // I love studying!
teacher.speak() // hhhhhh