Javascript JS嵌套继承

Javascript JS嵌套继承,javascript,inheritance,prototypal-inheritance,Javascript,Inheritance,Prototypal Inheritance,我无法正确覆盖继承的对象,不知道是否可以在这里插手。已经被困了3天了 说 然后 当我运行bobStu.serviceare()时我得到“新位置”,但是当我运行bobStu.getValue()时我得到“旧位置” 我正在传递这个bobStu.getValue()关闭一个需要调用重写方法的方法,但我无法让它这样做。你能解释一下为什么getValue()调用旧的serviceArea()?如何正确地做到这一点 我已经准备了很多次这篇文章,感觉它在告诉我一些事情,但是我太激动了,以至于我无法理解:(仅使

我无法正确覆盖继承的对象,不知道是否可以在这里插手。已经被困了3天了

然后

当我运行
bobStu.serviceare()时我得到
“新位置”
,但是当我运行
bobStu.getValue()时我得到
“旧位置”

我正在传递这个
bobStu.getValue()关闭一个需要调用重写方法的方法,但我无法让它这样做。你能解释一下为什么
getValue()
调用旧的
serviceArea()
?如何正确地做到这一点


我已经准备了很多次这篇文章,感觉它在告诉我一些事情,但是我太激动了,以至于我无法理解:(

仅使用
serviceare()
仅指
Person
范围内定义的
serviceare
函数:

function Person() { 
    function getValue(){
        serviceArea();  // this...
    }

    // ...always refers to this, independent of the object you're constructing
    function serviceArea() {
        alert('Old Location');
    }

    // also note this:
    this.getValue = getValue;
    this.serviceArea = serviceArea;
}
如果要使用子类实现的
serviceArea
方法,请使用
this.servicea()


此外,构造函数不应返回值;而是将值直接附加到
this
值。

您的
Person
是一个工厂函数,不是经典构造函数,它的作用域不关心属性。您可能会发现此线程中的答案很有用。
function Student() {};
Student.prototype = new Person();
Student.prototype.serviceArea = function() { alert('New Location'); };
var bobStu = new Student();
function Person() { 
    function getValue(){
        serviceArea();  // this...
    }

    // ...always refers to this, independent of the object you're constructing
    function serviceArea() {
        alert('Old Location');
    }

    // also note this:
    this.getValue = getValue;
    this.serviceArea = serviceArea;
}