目前我正在做这个:
foo.js
const FOO = 5;
module.exports = {
FOO: FOO
};
并在bar.js中使用它:
var foo = require('foo');
foo.FOO; // 5
还有更好的办法吗?在exports对象中声明常量会让人感到尴尬。
目前我正在做这个:
foo.js
const FOO = 5;
module.exports = {
FOO: FOO
};
并在bar.js中使用它:
var foo = require('foo');
foo.FOO; // 5
还有更好的办法吗?在exports对象中声明常量会让人感到尴尬。
当前回答
我认为const解决了大多数人寻找这个答案的问题。如果你真的需要一个不可变的常数,看看其他的答案。 为了保持所有内容的组织,我将所有常量保存在一个文件夹中,然后需要整个文件夹。
src / main.js文件
const constants = require("./consts_folder");
src consts_folder / index . js
const deal = require("./deal.js")
const note = require("./note.js")
module.exports = {
deal,
note
}
Ps.这里的交易和注意将在main.js的第一层
src / consts_folder笔记js。
exports.obj = {
type: "object",
description: "I'm a note object"
}
Ps. obj将是main.js的第二级
src / consts_folder成交js。
exports.str = "I'm a deal string"
Ps. str将是main.js的第二级
main.js文件的最终结果:
console.log (constants.deal); 输出:
{交易:{str: '我是一个交易字符串'},
console.log (constants.note); 输出:
备注:{obj: {type: 'object', description: 'I\'m a note object'}}
其他回答
作为一种替代方法,您可以将“常量”值分组在一个局部对象中,并导出一个返回该对象的浅克隆的函数。
var constants = { FOO: "foo" }
module.exports = function() {
return Object.assign({}, constants)
}
然后,如果有人重新分配FOO就无关紧要了,因为它只会影响他们的本地副本。
从技术上讲,const不是ECMAScript规范的一部分。此外,使用你已经注意到的“CommonJS Module”模式,你可以改变这个“常量”的值,因为它现在只是一个对象属性。(不确定这是否会级联需要相同模块的其他脚本的任何更改,但这是可能的)
要获得一个可以共享的真实常数,请检查Object。create、Object.defineProperty和Object.defineProperties。如果你设置writable: false,那么你的“常量”中的值不能被修改。:)
这有点啰嗦(但即使是这样也可以用一个小JS来改变),但你应该只需要为你的常量模块做一次。使用这些方法,您省略的任何属性都会默认为false。(与通过赋值定义属性相反,赋值默认所有属性为true)
所以,假设,你可以只设置value和enumerable,省略writable和可配置的,因为它们默认为false,我只是为了清晰起见才包含它们。
更新-我已经为这个用例创建了一个带有helper函数的新模块(node-constants)。
js——很好
Object.defineProperty(exports, "PI", {
value: 3.14,
enumerable: true,
writable: false,
configurable: false
});
js——更好
function define(name, value) {
Object.defineProperty(exports, name, {
value: value,
enumerable: true
});
}
define("PI", 3.14);
script.js
var constants = require("./constants");
console.log(constants.PI); // 3.14
constants.PI = 5;
console.log(constants.PI); // still 3.14
导入和导出(2018年可能需要像Babel这样的东西才能使用导入)
types.js
export const BLUE = 'BLUE'
export const RED = 'RED'
myApp.js
import * as types from './types.js'
const MyApp = () => {
let colour = types.RED
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
我建议使用webpack(假设你正在使用webpack)。
定义常量就像设置webpack配置文件一样简单:
var webpack = require('webpack');
module.exports = {
plugins: [
new webpack.DefinePlugin({
'APP_ENV': '"dev"',
'process.env': {
'NODE_ENV': '"development"'
}
})
],
};
通过这种方式,您可以在源代码之外定义它们,并且它们将在您的所有文件中可用。
从之前的项目经验来看,这是一个很好的方法:
在constants.js中:
// constants.js
'use strict';
let constants = {
key1: "value1",
key2: "value2",
key3: {
subkey1: "subvalue1",
subkey2: "subvalue2"
}
};
module.exports =
Object.freeze(constants); // freeze prevents changes by users
在main.js(或app.js等)中,使用如下:
// main.js
let constants = require('./constants');
console.log(constants.key1);
console.dir(constants.key3);