1.
function Login(base, req, res) {this._base = base;
this._req = req;
this._res = res;
this.title = 'Login_title';
this.description = 'Login_description';
}
Login.prototype = {
constructor: Login,
description: 'default_description',
auth: function (args) {
this._res.send(this.title + " => " + args + " , " + this.description);
}
};
module.exports = Login;
/**************************/ constructor version second
2.
class Login {constructor(base, req, res) {
this.base = base;
this.req = req;
this.res = res;
this.foo = 10;
}
auth(args) {
this.res.json(this.foo + ';;;' + args);
}
}
module.exports = Login;
****************************************************
var _constructor = require('./login');
var _object = new _constructor(base, req, res);
this.function = "auth";
if (typeof _object[this.function] === 'function') {
_object[this.function]("passed args");
}
***********************************************************
1.2 Accessing global values from objects
// constructor
var Class = function(global, value2) {
this.global = global;
}
// access using this.global in class methods
1.3 Factory pattern
// Constructor
var Class = function(value1, value2) { ... }
// Factory
Class.factory(value1) { return new Class(value1, "aaa"); }
// properties and methods
Class.prototype = { ... };
1.4 Sharing state between modules
var Common = {
util: require('util'),
fs: require('fs'),
path: require('path')
};
module.exports = Common;
// in other modules
var Common = require('./common.js');
1.5 Singleton class (added Feb 2011)
var Singleton = (function() {
var private_variable = 'value';
function private_function() {
...
}
function public_function() {
...
}
return {
public_function: public_function
};
})();
No comments:
Post a Comment