2017-09-17 5 views
0

以下のコードを理解するために何が起こっているのか、また何かリンクを説明できますか?ここでJavascriptの匿名機能がオブジェクトを追加しています

function createApplication() { 
    var app = function(req, res, next) { 
    app.handle(req, res, next); 
    }; 

    mixin(app, EventEmitter.prototype, false); 
    mixin(app, proto, false); 

    // expose the prototype that will get set on requests 
    app.request = Object.create(req, { 
    app: { configurable: true, enumerable: true, writable: true, value: app } 
    }) 

    // expose the prototype that will get set on responses 
    app.response = Object.create(res, { 
    app: { configurable: true, enumerable: true, writable: true, value: app } 
    }) 

    app.init(); 
    return app; 
} 

アプリの種類は、関数であるが、他のオブジェクトが割り当てられている場合、その後復帰した後、これらのオブジェクトにアクセスする方法

答えて

1

少し説明:

var app = function(req, res, next) { 
    app.handle(req, res, next); 
    }; 

appは、関数という名前です

後に表示されるmixinは、元のappオブジェクトのプロパティを拡張するために使用されます。

mixin(app, EventEmitter.prototype, false); 
mixin(app, proto, false); 

以下、このコードは、あなたがより多くに関する情報のためdocsを見ることができapp.request上のいくつかのプロパティを定義します。

Object.defineProperties()メソッドは、既存の プロパティを新規定義または変更してオブジェクトを直接返します。

では、JS関数はオブジェクトなので、独自のプロパティを持つことができます(この場合はrequest)。

// expose the prototype that will get set on requests 
    app.request = Object.create(req, { 
    app: { configurable: true, enumerable: true, writable: true, value: app } 
    }) 

    // expose the prototype that will get set on responses 
    app.response = Object.create(res, { 
    app: { configurable: true, enumerable: true, writable: true, value: app } 
    }) 

コードがappオブジェクト初期化:私は少しだ

app.init(); 
+0

をこれらのオブジェクトは –

+0

おかげ値にアクセスする方法を、あなたは名前の機能上のゲッターとセッターについてご説明しながら、混乱させ、ゲッターとしてのオブジェクトを割り当て、私は編集を行いました。基本的にJS関数はオブジェクトなので、独自のプロパティ(この場合は 'request')を持つことができるので、app.requestを使ってアクセスすることができます。 – GibboK

+0

ありがとうございました –

関連する問題