I am trying to write a routing framework of nodejs, I need to add some helper methods for ServerRequest and ServerResponse. I notice express has change it way from modify prototype to
express/response.js
var res = module.exports = {
__proto__: http.ServerResponse.prototype
};
res.redirect = function (url) {
...
}
And express/middlewares.js
expressInit = function(req, res, next) {
// ...
res.__proto__ = app.response;
next()
}
But in my framework, I just like to do it simple:
http.ServerResponse.prototype.redirect = function(url) {
...
}
I don’t know if there is something I don’t know of why express change the style of override.
Note that the
__proto__pseudo-attribute is non-standard so it’s use was never really appropriate; moreover, it is now considered “deprecated” according to the Mozilla Developer Network (MDN).However, the
prototypeproperty of Functions is a standard part of the language and its use is always safe.Always use
MyFunction.prototype, never usemyObject.__proto__.