简体   繁体   English

Javascript继承-对子方法的调用始终会调用父方法

[英]Javascript inheritance - call to child method always invokes method of parent

This is my first time working with inheritance in Javascript. 这是我第一次使用Javascript继承。 I have a classical parent-child relation between an object "LatchOn" that does one thing, and an object "LatchOnSlider" that has all the functionality of the base class and then some more. 我在做一件事的对象“ LatchOn”和具有基类的所有功能以及其他功能的对象“ LatchOnSlider”之间具有经典的父子关系。 stripped down code: 精简代码:

 /*creating the instances*/ var latchon = NewLatchOn(); //arguments not important for this var latchonslider = NewLatchOnSlider(); //factory function function NewLatchOn(element_id, container_id, screenpos, release) { var newLatchOn = new LatchOn(element_id, container_id, screenpos, release); newLatchOn.startTimer(); } function LatchOn(element_id, container_id, screenpos, release) { //initialise stuff } LatchOn.prototype.startTimer = function() { var that = this; setInterval(function(){that.checkPos()}, 10); } LatchOn.prototype.checkPos = function() { //do stuff } LatchOnSlider.prototype.constructor = LatchOnSlider; //factory function function NewLatchOnSlider(element_id, container_id, image_class, screenpos) { LatchOnSlider.prototype = Object.create(LatchOn.prototype); var newSlider = new LatchOnSlider(element_id, container_id, image_class, screenpos); newSlider.startTimer(); return newSlider; } function LatchOnSlider(element_id, container_id, image_class, screenpos) { LatchOn.call(this, element_id, container_id, screenpos, "CONTAINER"); //initialise own stuff } LatchOnSlider.prototype.startTimer = function() { var that = this; setInterval(function(){that.checkPos()}, 10); } /*keeps track of when to switch images*/ LatchOnSlider.prototype.checkPos = function() { alert("child process called"); } 

The objects have to listen to the scrollposition in the browser and act according to that information. 对象必须侦听浏览器中的滚动位置并根据该信息进行操作。 So they have a timer running. 因此,他们有一个计时器运行。 But as it wouldn't be optimal for two timers running for the inherited object, I figured I could just run one timer that calls the function on the child object, which will invoke the base function and then do its own stuff (invocation of base function not yet in the code. I haven't gotten that far yet...). 但是由于对于为继承对象运行的两个计时器不是最佳选择,我认为我可以只运行一个计时器来调用子对象上的函数,后者将调用基本函数,然后执行自己的工作(调用base函数尚未在代码中。我还没走那么远...)。

In order to not have to start the timer separately for every instance, I made factory functions to initialise the objects and start their timers. 为了不必为每个实例分别启动计时器,我使用了工厂函数来初始化对象并启动其计时器。 And this is where I'm stuck. 这就是我遇到的问题。 No matter what I do, if I call startTimer on the LatchOnSlider object, the function of the parent gets called instead. 无论我做什么,如果我在LatchOnSlider对象上调用startTimer,则会调用父函数。 I've looked through at least five different tutorials and tried all their syntax variations, but nothing fixes the problem. 我浏览了至少五本不同的教程,并尝试了其所有语法变体,但没有任何方法可以解决问题。 I've walked through it in firebug, the process works as expected up to that point. 我已经在Firebug中进行了遍历,到目前为止,该过程可以按预期进行。 The constructor of LatchOnSlider gets called, the constructor of the base object gets called from there, everything initialises properly, just when LatchOnSlider.startTimer() is called, LatchOn.startTimer() gets executed instead. 调用LatchOnSlider的构造函数,从那里调用基础对象的构造函数,一切都正确初始化,仅当调用LatchOnSlider.startTimer()时,才执行LatchOn.startTimer()。 I'm pretty much out of ideas as to what might be the problem at this point. 关于此时可能存在的问题,我几乎没有主意。

This line is the problem: 这行是问题所在:

 function NewLatchOnSlider(element_id, container_id, image_class, screenpos) { LatchOnSlider.prototype = Object.create(LatchOn.prototype); 

Here, you are creating a new prototype object every time an instance is created. 在这里,每次创建实例时都在创建一个新的原型对象。 Those new prototypes do not have the methods you had assigned on the "old" LatchOnSlider.prototype before, and your instances will not inherit them - only the LatchOn.prototype methods indirectly. 这些新原型不具有您之前在“旧” LatchOnSlider.prototype上分配的方法,并且您的实例将不会继承它们-只能间接地继承LatchOn.prototype方法。

You will need to keep the constructor declaration and the prototype initialisiation separate from your factory function: 您需要将构造函数声明和原型初始化与工厂函数分开:

function NewLatchOnSlider(element_id, container_id, image_class, screenpos) {
    var newSlider = new LatchOnSlider(element_id, container_id, image_class, screenpos);
    newSlider.startTimer();
    return newSlider;
}

function LatchOnSlider(element_id, container_id, image_class, screenpos) {
    LatchOn.call(this, element_id, container_id, screenpos, "CONTAINER");
    // initialise own stuff
}

LatchOnSlider.prototype = Object.create(LatchOn.prototype);
LatchOnSlider.prototype.constructor = LatchOnSlider;
LatchOnSlider.prototype.startTimer = function() { … }
LatchOnSlider.prototype.…

For assignments, order matters :-) 对于分配,订购很重要:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM