构造函数class 普通函数function
class Person {
//静态属性,放置在construction中
construction(name , age){
this.name = name
this.age = age
}
//原型方法
eat(){
return `$(this.name)在吃饭`
}
sleep(){
return `$(this.name)在睡觉`
}
}
let p1 = new Person ("Tom", 25)
//本质上js的继承逻辑是原型链
-------------------------------------
function Person (name,age) {
this.name = name;
this.age = age;
}
Person.prototype.eat = function () {
return this.name+"在吃"
}
Person.prototype.sleep = function () {
return this.name+"在睡"
}
let p1 = new Person("Kim", 20)
let p2 = new Person("Ben", 30)
console.log (p1.eat === p2.eat)//true
原文:https://www.cnblogs.com/Topazqin/p/13287250.html