代码之家  ›  专栏  ›  技术社区  ›  DLF85

通过伪经典实例化(JavaScript)掌握原型继承

  •  3
  • DLF85  · 技术社区  · 9 年前

    我正试图通过JavaScript通过继承来传递一个测试套件。下面是我迄今为止的代码片段:

    var Infant = function() {
        this.age  = 0;
        this.color = 'pink';
        this.food = 'milk';
    
    };
    Infant.prototype.eat = function(){
        return this.eat;
    }
    
    
    var Adolescent = function() {
    
        this.age = 5;
        this.height = 'short';
        this.job = 'keep on growing';
    
    };
    

    我想继承Infant类的food属性和eat方法,但我的尝试失败了。我最初的想法是分配这个。青少年=婴儿。食物但这不管用。我知道我需要把婴儿设定为超级级,但我正在旋转车轮

    1 回复  |  直到 9 年前