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

在Javascript中,如何继承和扩展被继承的方法?,javascript,oop,backbone.js,prototype,Javascript,Oop,Backbone.js,Prototype,很遗憾,我已经到处搜索了,但没有找到这个问题的副本 文件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 wh

很遗憾,我已经到处搜索了,但没有找到这个问题的副本

文件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

做:

图形模块:

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....
  }
});
子图模块:

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:


你能给出你期望的更多细节吗?我想你可以做var SubGraph=Graph.extend…,以设置继承,但我不确定。。。我在pastIs中只处理过主干一次,有没有办法在move方法中扩展语句?例如:Graph的move方法包含一个语句this.model.save{},它保存x和y坐标,我想从子图的move方法中扩展该语句或将其替换为inlcude z坐标;这是graph对象当前包含的内容:move:function{this.model.save{x-coord:this.deltaX,y-coord:this.deltaY}您不能扩展它,因为它是一个函数而不是构造函数。如果您使用move作为var moveA=new move,您可以执行move.prototype.newMethod=function…但是看起来情况并非如此。我需要做些什么来移动:function{…}在graph.js中,这在技术上被认为是一个构造函数吗?还是仅仅执行var moveA=new move;move:function{//some extra function}在subgraph.js中?感谢您花时间回复!如果您不需要修改move,那么在执行Graph.extend时您已经拥有了它。您的问题是重新修改它-但是如果您想修改move,那么为什么首先需要扩展Graph?您通常会进行扩展,因为您不需要修改大多数的方法您扩展的move类有几个语句,我想在子图和其他视图中继承和使用,只是需要更改的“this.model.save…”语句