向其添加对象的Javascript匿名函数
你能解释一下下面的代码是怎么回事吗向其添加对象的Javascript匿名函数,javascript,node.js,express,Javascript,Node.js,Express,你能解释一下下面的代码是怎么回事吗 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 req
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;
}
这里的应用程序类型是函数,但分配了其他对象,然后返回如何访问这些对象
app
被命名为函数:
var app = function(req, res, next) {
app.handle(req, res, next);
};
之后看到的mixin
用于扩展原始app
对象的属性
mixin(app, EventEmitter.prototype, false);
mixin(app, proto, false);
下面,这段代码定义了app.request
上的一些属性,您可以查看更多信息
defineProperties()方法定义新属性或修改现有属性
属性,返回对象
在JS中,函数是对象,因此可以有自己的属性(在本例中为request
)
代码初始化app
对象:
app.init();
当你解释名字函数上的getter和setter,并将对象指定为getter,然后如何访问这些对象值时,我有点困惑。hanks,我做了一个编辑,基本上在JS中,函数是对象,因此可以有自己的属性(在本例中为
请求
),所以你可以使用app.request来访问它。@SunilPatel不客气,我很高兴它有用。请不要忘记通过单击左侧的勾号/箭头向上图标接受或向上投票我的答案。谢谢你的合作。
app.init();