2012-07-20 69 views
1

我周围搜索,但不幸的是,还没有找到这个问题的重复。在JavaScript中,如何继承和扩展正在继承的方法?

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

你如何在不破坏现有的延长继承的属性?

+0

你能提供更多关于你的期望的细节吗? – Lucero 2012-07-20 21:43:19

+0

我认为你可以做'var SubGraph = Graph.extend(...',为了设置继承,但我不确定...(我过去只有一次处理骨干) – 2012-07-20 21:45:24

回答

0

看起来你正在使用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.... 
    } 
}); 

或者,如果你没有定义很多依赖关系,不包括require

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

是有一种方法可以在移动方法中扩展语句吗?EG:Graph的移动方法包含一个声明this.model.save({}),它保存x和y坐标,并且我希望从子图的移动方法到扩展语句或将其替换为包含z坐标;这是图对象当前包含的内容:move:function(){this.model.save({x-coord:this.deltaX,y-coord:this.deltaY} ) – 2012-07-20 22:25:43

+0

你不能扩展它,因为它不是一个构造函数,如果你使用'move'作为'var moveA = new move()',你可以执行'move.prototype.newMethod = function()...'但看起来并非如此 – 2012-07-20 22:41:59

+0

只需要清楚,我是否需要做任何动作:function(){...}在graph.js(is技术上认为是构造函数?)或者只是做var moveA = new move();在subgraph.js中移动:function(){//一些额外的功能}?感谢您花时间回复! – 2012-07-20 23:13:26