在Javascript中,如何继承和扩展正在继承的方法?(In Javascript, how can you inherit and extend a method that is being inherited?)

我已经四处寻找,但遗憾的是没有找到这个问题的副本。

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

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

I've searched around, but haven't found a duplicate of this question, unfortunately.

File1:

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

File 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 })

How do you extend the inherited properties without destroying the existing ones?

最满意答案

看起来你正在使用Require.js

做:

图形模块:

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

SubGraph模块:

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.... } });

Looks like you're using Require.js

Do:

Graph module:

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

SubGraph module:

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

Or if you don't define a lot of dependencies, don't include require:

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

更多推荐