class Person {
constructor(name, age) {
this.name = name
this.age = age
}
obj = {
a: '小胡',
b: '小李'
}
getInfo() {
console.log('父类中的获取信息方法')
return `${this.name}今年${this.age}了`
}
static staticFunc() {
console.log('这是一个静态方法')
}
}
const p1 = new Person('王磊', 19)
Person.prototype.getAge = function () {
console.log(this, this.age)
}
Object.assign(Person.prototype, {
getName() {
console.log(this, this.age)
},
setName(val) {
this.name = val
console.log(this, this.name)
}
})
p1.__proto__.eat = function () {
console.log(this, '此方法通过__proto__创建')
}
class Student extends Person {
constructor(name, age, hobbie) {
super()
this.name = name
this.age = age
this.hobbie = hobbie
}
getInfo() {
return `${this.name}今年${this.age}了`
}
hobbies() {
return `${this.name}喜欢${this.hobbie}`
}
}
const stu1 = new Student('小胡', 20, '打篮球')
function Animals(name) {
this.name = name
}
const cat = new Animals('Cat')
Animals.prototype.eat = function() {
console.log(`${this.name}吃饭了`)
}
cat.__proto__ = Animals.prototype
for(var prop in cat){
console.log(prop);
}
console.log(Object.keys(cat))
console.log(Object.keys(Animals.prototype))
console.log(Object.getOwnPropertyNames(Animals.prototype))
console