在我的文件./functions/login_registration_functions.js
我有以下代码
var methods = {
check_if_not_null: function (item_to_be_checked, item_to_store_the_checked_item) {
if(item_to_be_checked != null){
item_to_store_the_checked_item = item_to_be_checked;
}
}
};
module.exports = methods;
我在我的routes.js
文件中调用它
var log_reg_funcs = require('./functions/login_registration_functions.js');
我正在调用 put
调用中的方法
log_reg_funcs.check_if_not_null(req.body.title, request.title);
但它不起作用。当我用下面的代码替换上面的代码时:
if(req.body.title != null){
request.title = req.body.title;
}
效果很好
为什么我失踪了?
最佳答案
当你这样做时:
item_to_store_the_checked_item = item_to_be_checked;
当 item_to_store_the_checked_item
是函数的参数时,您不会更改调用函数时使用的原始变量,而只会更改函数中传递值的本地副本。
如果您传递并反对并更改其属性之一,那么它将按您的预期工作。
例如,如果您有:
var methods = {
check_if_not_null: function (item_to_be_checked, object_to_store, object_key) {
if(item_to_be_checked != null){
object_to_store[object_key] = item_to_be_checked;
}
}
};
然后你就可以做到:
log_reg_funcs.check_if_not_null(req.body.title, request, 'title');
(JavaScript 使用下划线而不是驼峰命名法很奇怪,但我在这里保留了你的风格)
关于javascript - NodeJS - 我的方法不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43608708/