javascript - 我怎样才能覆盖 JavaScript 中的父类(super class)

标签 javascript

我这里有一个父类(super class):

// SUPERCLASS
var toWalk = function(bottom, right, space) {
  this.$node = $('<span class="walker"></span>');
  this.space = space;
  this.step();
  this.setPosition(bottom, right);
};

toWalk.prototype.step = function() {
  setTimeout(this.step.bind(this), this.space);
};

toWalk.prototype.setPosition = function(bottom, right) {
  var styleSettings = {
    bottom: bottom,
    right: right
  };

  this.$node.css(styleSettings);
};

这基本上设置了一个步行 Angular 色的步行者跨度。 class walker 有自己的 CSS。现在我想要实现的是覆盖它的 CSS 以及它的位置(底部和右侧)所以我所做的是在我的新实例上我放置了一个新节点,它有一个也有自己的 CSS 的类运行器并应用实例父类(super class)的参数,但这不会覆盖我在这个新实例的父类(super class)上拥有的内容。

// SUBCLASS

var toRun = function(bottom, right, space) {
  this.$node = $('<span class="runner"></span>');
  toWalk.apply(this, arguments);
};


toRun.prototype = Object.create(toWalk.prototype);

toRun.prototype.constructor = toRun;

toRun.prototype.step = function() {
  toWalk.prototype.step.call(this);

  this.$node.toggleClass('size');

};

toWalk.prototype.setPosition = function(bottom, right) {
  var styleSettings = {
    bottom: bottom,
    right: right
  };

  this.$node.css(styleSettings);
};

知道我做错了什么吗?我怎样才能同时覆盖底部和右侧等位置?

最佳答案

将子类的构造函数改成...

var toRun = function(bottom, right, space) {
  toWalk.apply(this, arguments);
  this.$node = $('<span class="runner"></span>');
};

请注意,我调换了顺序,因此您在 调用 super 后覆盖了 this.$node。在您声明 this.$node 然后调用构造函数(使用相同的 this)之前,它覆盖了您刚刚在 this.$node 中设置的内容

关于javascript - 我怎样才能覆盖 JavaScript 中的父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48197553/

相关文章:

javascript - Webaudio,播放声音两次

javascript - 无法将对象推送到数组中

javascript - 为什么knockout-secure-binding会遇到这个语法错误

javascript - 当在字符串中找到部分正确的 Protractor 时,将预期结果设置为 true

javascript - 将文本分割为第一个 ";",然后再次分割为第二个 ";"

javascript - 如何从列表框中选择多个值

javascript - 从具有特定参数的数组中检索对象

Javascript -- 将窗口高度增加一个像素 -- Google Chrome

javascript - OLOO如何访问私有(private)变量

javascript - 使用 setTimeout 暂停 REST API 调用