Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS 如何实现继承 #24

Open
MaMaFish opened this issue Jun 18, 2020 · 0 comments
Open

JS 如何实现继承 #24

MaMaFish opened this issue Jun 18, 2020 · 0 comments

Comments

@MaMaFish
Copy link
Owner

MaMaFish commented Jun 18, 2020

ES5 寄生组合继承

function Father() {
    this.color = 'yellow'
}
function Child() {
    this.name = 'childName'
    Father.call(this)
}
Child.prototype.__proto__ = Father.prototype 
Child.prototype.constructor = Child
var child1 = new Child()
console.log(child1)

Screen Shot 2020-06-18 at 10 53 44

ES6 的继承实现

class Father {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    getName() {
        return this.name
    }

}
class Child extends Father {
    constructor(name, age, job) {
        super(name, age)
        this.job = job
    }
    getJob() {
        return this.job
    }
    getNameAndJob() {
        return super.getName() + this.getJob()
    }
}
var a = new Child('Tom', '20', 'IT')
console.log(a.name)    ==>Tom
console.log(a.age)   ==>20
console.log(a.job)   ==>IT
console.log(a.getName())   ==>Tom
console.log(a.getJob())   ==>IT
console.log(a.getNameAndJob())   ==>TomIT
@MaMaFish MaMaFish changed the title 第24题:JS如何实现继承 JS 如何实现继承 Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant