相关疑难解决方法(0)

在构造函数中分配原型

我有这个代码:

var MyClass = function(b) {
    this.a = b;
    this.getA = function() {
        return that.a;
    }
}

var SecondClass = function(b) {
    this.prototype = new MyClass(b);
    this.getB = function() {
        return 6;
    }
}

var a = new SecondClass(2);
console.log(a.getA());
Run Code Online (Sandbox Code Playgroud)

输出告诉我a没有名为getA()的方法

我假设在SecondClass的构造函数中执行this.prototype = new MyClass()会导致它从MyClass的inhert方法?

我确信有更好的方法可以做到这一点,但我试图理解prototype关键字的行为.

javascript

10
推荐指数
1
解决办法
3928
查看次数

标签 统计

javascript ×1