我正在做一个早午餐项目。
我有两个javascript文件,比方说A.js和B.js
A.js:
function replacer(key, value) {
if (typeof value === 'number' && !isFinite(value)) {
return String(value);
}
return value;
};
B.js:
atts = ...
json = JSON.stringify(atts, replacer);
在我的HTML中,我做了:
<script type="text/javascript">
require('scripts/front/A');
require('scripts/front/B');
</script>`
当执行javascript B时,我没有定义替换器。
可以从不同的文件中调用函数吗?
答案 0 :(得分:1)
在a.js
module.exports = function (key, value) {
if (typeof value === 'number' && !isFinite(value)) {
return String(value);
}
return value;
};
在b.js中:var replacer = require('path/to/a.js');