在Javascript中,如何继承和扩展正在继承的方法

In Javascript, how can you inherit and extend a method that is being inherited?

本文关键字:继承 方法 扩展 Javascript 何继承      更新时间:2023-12-08

我到处找过,但很遗憾没有找到这个问题的重复。

文件1:

var Graph = Backbone.View.extend({
  move: function() {
    //some stuff
  }, //more stuff
})

文件2:

define ([ './graph.js' ]), function( graph ) {
  var SubGraph = Backbone.View.extend({
// this file needs to inherit what is within the move method but extend it to include other stuff
   })

如何在不破坏现有属性的情况下扩展继承的属性?

看起来您正在使用Require.js

Do:

图形模块:

define(function() {
  return Backbone.View.extend({
    move: function() {
    //some stuff
  }
});

子图模块:

define(['require', './graph'], function(require) {
  var Graph = require('./graph');
  return Graph.extend({
    // this file needs to inherit what....
  }
});

或者,如果你没有定义很多依赖项,就不要包括require:

define(['./graph'], function(Graph) {
  return Graph.extend({
    // this file needs to inherit what....
  }
});