2

こんにちは、私はノードが初めてで、MVC アプリを構築しようとしています。コントローラーとモデルについては、utils.inherits を使用して基本クラスとサブクラスを作成できました。ビューについては、base、html/json、module の 3 つのレベルを作成したいと思います。各レベルには、インスタンスの作成時に呼び出す必要があるコンストラクトと呼ばれる関数があり、最上位でそれを呼び出すと、各レベルに連鎖する必要があります。

ベース ビュー:

function Base_view( ) {
    this._response = null;
};

Base_view.prototype.construct = function( res ) {
    this._response = res;
};

HTML ビュー:

var util = require( 'util' ),
    Base_view = require( './view' );

function Html_view( ) {
    Base_view.apply( this, arguments );
}

util.inherits( Html_view, Base_view );

Html_view.prototype.construct = function( res, name ) {
    this.constructor.super_.prototype.construct.apply( this, arguments );
};

モジュール ビュー:

var util = require( 'util' ),
    Html_view = require( './../base/html' );

function Main_view( ) {
    Html_view.apply( this, arguments );
}

util.inherits( Main_view, Html_view );

Main_view.prototype.construct = function( ) {
    this.constructor.super_.prototype.construct.apply( this, arguments );
};

モジュール ビューの次の行は、未定義のエラーを生成します。

this.constructor.super_.prototype.construct.apply( this, arguments );

一度だけサブクラス化すると、親クラスのコンストラクト メソッドが正しく呼び出されます。複数回延長するにはどうすればよいですか?

この投稿では: util.inherits - 代替または回避策変更された utils.inherits メソッドがありますが、それを行うように見えますが、使用方法がわかりませんか? モジュールで両方のクラスを要求し、3つすべてをパラメーターとして配置しようとしました。

ありがとう!

4

1 に答える 1

6

偽の呼び出し可能なコンストラクターを追加し、コンストラクターとして func def を使用する試みを削除することで、機能しました。util の require は無視してください。これは、いくつかの標準的な util 関数といくつかの独自の関数を含めるための単なるラッパーです。組み込みメソッドを呼び出すだけです。

/controllers/base/view.js:

function Base_view( res ) {
    this._response = res;
};

/controllers/base/html.js:

var util = require( '../../helpers/util' ),
    Base_view = require( './view' );

function Html_view( res, name ) {
    Base_view.apply( this, arguments );
    this._name = name;
};

util.inherits( Html_view, Base_view );

/コントローラー/メイン/html.js:

var util = require( '../../helpers/util' ),
    Html_view = require( './../base/html' );

function Main_view( res, name ) {
    Html_view.apply( this, arguments );
};

util.inherits( Main_view, Html_view );
于 2013-09-19T15:49:36.683 に答える