【问题标题】:Declaring libraries as constants将库声明为常量
【发布时间】:2014-05-01 18:23:30
【问题描述】:

在 NodeJS 世界中,我们需要使用 require 函数的模块:

var foo = require ("foo");

在 JavaScript 中(也在 NodeJS 中)我们有 const 关键字来创建一个常量:

const

创建一个常量,该常量对于声明它的函数来说可以是全局的或局部的。常量遵循与变量相同的范围规则。

例子:

$ node
> const a = 10
undefined
> a
10
> a = 7
7
> a
10

我的问题是:要求库作为常量会更好吗?

例子:

const foo = require ("foo")
    , http = require ("http")
    ;

/* do something with foo and http */

在需要库时使用const 代替var 有什么坏/好效果吗?

【问题讨论】:

  • +1 因为我不知道 const 关键字已添加到 javascript。
  • @JamesDuffy:这不是支持这个问题的好理由。
  • @LightnessRacesinOrbit stackoverflow.com/help/privileges/vote-up "我什么时候应该投票?每当您遇到您认为特别有用的问题、答案或评论时,请投票!"这个问题对我很有用,因为我学到了一些东西。
  • @JamesDuffy 我同意。我也投票赞成包含对我来说新事物的问题。感谢您的支持。 :-)
  • @JamesDuffy:这对帮助我拖延几分钟很有用,但那不是那篇文章的意图!我认为这很清楚。否则,您不妨招募所有您认识的从未编程过因此从未听说过 JavaScript 的人,让他们投票赞成这个问题,因为他们学到了“一些东西”,然后声称您正在本着这个社区的精神行事。什么废话。

标签: javascript node.js variables constants


【解决方案1】:

事实证明,使用const 而不是var 来实现依赖关系已成为一种常见的做法。至少在 Node.js 源代码中是这样的:

所以,我想这是一个很好的做法。我也在my modules开始使用它。

【讨论】:

    【解决方案2】:

    NodeJS 没有对需要 const 的库进行任何优化 - require 是一个简单的非本地函数,对于分配给它的变量的类型一无所知。有require的源码:

    Module.prototype.require = function(path) {
      assert(util.isString(path), 'path must be a string');
      assert(path, 'missing path');
      return Module._load(path, this);
    };
    
    Module._load = function(request, parent, isMain) {
      if (parent) {
        debug('Module._load REQUEST  ' + (request) + ' parent: ' + parent.id);
      }
    
      var filename = Module._resolveFilename(request, parent);
    
      var cachedModule = Module._cache[filename];
      if (cachedModule) {
        return cachedModule.exports;
      }
    
      if (NativeModule.exists(filename)) {
        // REPL is a special case, because it needs the real require.
        if (filename == 'repl') {
          var replModule = new Module('repl');
          replModule._compile(NativeModule.getSource('repl'), 'repl.js');
          NativeModule._cache.repl = replModule;
          return replModule.exports;
        }
    
        debug('load native module ' + request);
        return NativeModule.require(filename);
      }
    
      var module = new Module(filename, parent);
    
      if (isMain) {
        process.mainModule = module;
        module.id = '.';
      }
    
      Module._cache[filename] = module;
    
      var hadException = true;
    
      try {
        module.load(filename);
        hadException = false;
      } finally {
        if (hadException) {
          delete Module._cache[filename];
        }
      }
    
      return module.exports;
    };
    

    更多时候库是一个对象(我认为你没有像这样的库module.exports = 10)。

    如果对象声明为 const,您也可以更改对象的所有字段(如果您想获得真正的 const 对象,请使用 Object.freeze(someObject))。

    结论:效果与公共变量相同。 Link 到 NodeJS 中使用的 V8 变量声明函数

    【讨论】:

    • 你能提供官方参考来支持这个答案吗?
    • "效果与普通变量相同" - 不,因为您不能覆盖变量,这将允许进行一些静态优化。问题不在于require() 及其返回的内容,而在于const
    • NodeJS 对需要 const 的库没有任何优化 - 你能证明这一点吗?那么const的目的是什么?
    • @IonicăBizău:仅仅因为 NodeJS 不是为利用 const 的优势而设计的,并不意味着没有。
    • @LightnessRacesinOrbit 这是我的问题:使用const myLib = require ("foo"); 时有什么好/坏影响?
    猜你喜欢
    • 2012-01-08
    • 2010-10-15
    • 2017-03-09
    • 2011-09-18
    • 2011-03-11
    • 1970-01-01
    • 2011-10-13
    • 2020-11-22
    • 2014-07-20
    相关资源
    最近更新 更多