还有其他相关的答案,但我的问题是不同的,因为我正在使用基于原型的方法。
Authorization.js
var Authorization = function() {};
Authorization.prototype.requestPermissions = function(access_token, authToken, publicationId) {
};
Authorization.prototype.verifyRoles = function(access_token, authToken, roles, permissions) {
};
module.exports = new Authorization();
App.js
authorization = require("../../authorizationService/Authorization");
authorization.requestPermissions("abcd", "1234", "pub1234");
authorization.verifyRoles("abcd", "1234", "param1", "param2");
注意在每个函数中重复使用access_token,authToken 的问题,我的问题是每次调用一次又一次地传递“abcd”,“1234”。
理想情况下,我想将access_token和authToken一次传递给模块,然后继续将其用于每个后续调用。
我想参数化模块,也许在构造函数中传递access_token和authToken,同时保持模块原型。请告知重写模块。
答案 0 :(得分:2)
您应该从模块而不是实例返回构造函数:
Authorization.js
var Authorization = function(access_token, authToken) {
this.access_token = access_token;
this.authToken = authToken
};
Authorization.prototype.requestPermissions = function(publicationId) {
};
Authorization.prototype.verifyRoles = function(roles, permissions) {
};
module.exports = Authorization;
App.js
Authorization = require("../../authorizationService/Authorization");
authorization = new Authorization("abcd", "1234");
authorization.requestPermissions("pub1234");
authorization.verifyRoles("param1", "param2");