Giter Site home page Giter Site logo

Comments (8)

DamomHd avatar DamomHd commented on July 17, 2024

通过call实现

function Parent1(){
    this.name = 'parent1'
}
function Child1(){
    Parent1.call(this)
    this.type = 'child1'
}
console.log(new Child1)

from interview-question.

DamomHd avatar DamomHd commented on July 17, 2024

通过原型链

function Parent1(){
    this.info = [1,2,3]
    this.name = 'parent1'
}
function Child1(){
    this.type = 'child1'
}
Child1.prototype = new Parent1()

console.log(new Child1())

from interview-question.

DamomHd avatar DamomHd commented on July 17, 2024

通过原型链和call

function Parent1(){
    this.info = [1,2,3]
    this.name = 'parent1'
}
function Child1(){
    Parent1.call(this)
    this.type = 'child1'
}
Child1.prototype = new Parent1()

console.log(new Child1())

from interview-question.

DamomHd avatar DamomHd commented on July 17, 2024

组合继承优化1

function Parent1(){
    this.info = [1,2,3]
    this.name = 'parent1'
}
function Child1(){
    Parent1.call(this)
    this.type = 'child1'
}
Child1.prototype = Parent1.prototype

from interview-question.

DamomHd avatar DamomHd commented on July 17, 2024

最佳推荐使用,寄生组合继承

function Parent1(){
    this.info = [1,2,3]
    this.name = 'parent1'
}
function Child1(){
    Parent1.call(this)
    this.type = 'child1'
}
Child1.prototype = Object.create(Parent1.prototype)
Child1.prototype.constructor = Child1

from interview-question.

DamomHd avatar DamomHd commented on July 17, 2024

通过call实现

function Parent1(){
    this.name = 'parent1'
}
function Child1(){
    Parent1.call(this)
    this.type = 'child1'
}
console.log(new Child1)

问题:子类能够拿到父类属性值,父类原型对象一旦存在方法则子类无法继承

from interview-question.

DamomHd avatar DamomHd commented on July 17, 2024

通过原型链

function Parent1(){
    this.info = [1,2,3]
    this.name = 'parent1'
}
function Child1(){
    this.type = 'child1'
}
Child1.prototype = new Parent1()

console.log(new Child1())

问题:两个实例使用同一个原型对象,改变属性多个实例也会改变

from interview-question.

DamomHd avatar DamomHd commented on July 17, 2024

通过原型链和call

function Parent1(){
    this.info = [1,2,3]
    this.name = 'parent1'
}
function Child1(){
    Parent1.call(this)
    this.type = 'child1'
}
Child1.prototype = new Parent1()

console.log(new Child1())

问题:Parent1构造函数会多执行一次

from interview-question.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.