在同一文档中重用 export.function
Reuse export.function in same document
我正在一个平均.js堆栈中构建一个设置模块。我想重用现有功能。但是怎么做呢?下面是一些演示代码:
controller-x.js
var mongoose = require( 'mongoose' ),
errorHandler = require( './errors' ),
time = require( 'time' ),
_ = require( 'lodash' );
exports.create = function( req, res ) {
// create something
};
exports.save = function( req, res ) {
// save something
};
exports.update = function( req, res ) {
// update something
};
如何(重新)使用更新或创建中的"保存"功能,或其他功能?
您可以在
代码中的其他位置创建函数,并在调用exports.save
函数时简单地引用它,如下所示:
var someFunction = function(req, res) {
...logic for your save function...
};
exports.save = someFunction(req, res);
exports.update = function(req, res) {
someFunction(req, res);
... And whatever other logic you want to use...
};
您可以创建一个私有方法:
controller-x.js
var mongoose = require( 'mongoose' ),
errorHandler = require( './errors' ),
time = require( 'time' ),
_ = require( 'lodash' );
var save = function( req, res ) {
// save something
};
exports.create = function( req, res ) {
// create something
save();
};
exports.save = save;
exports.update = function( req, res ) {
// update something
};
相关文章:
- 直接在函数声明上使用function.prototype.bind
- jQuery:.click(function(){(element),collapse('show',f
- 如果连接类型为none,则Javascript Function Only警报
- onLoad function phonegap android
- reducers在redux中得到Function not Object,what'it’他错了
- WIll window.addEventListener(“加载”,function(),false);等待浏览器自动填
- $(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进
- Javascript onunload function
- 在init function()中调用getElementById(“..”),而不是想用它做点什么的函数
- spyOn:应为间谍,但得到了Function
- (typeof variable === “function”) 和 jQuery.isFunction() 有什么区别
- this内部的值返回this.each(function(){})
- 如何在JavaScript中创建类似于something.function()的函数
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- 控制台中的“function floor(){[本机代码]}”是什么
- ASPX Jquery 1.11 $(Document).ready(function() {} was skipped
- Function.prototype.call和Function.protoype.all只应用一个参数
- 将angular javascript文件包装为“;(function(){..[js-code here]…})()&
- 从Function触发JQuery UI日期选择器,并将日期存储到变量中
- 在同一文档中重用 export.function