简体   繁体   English

CommonJS模块(带有nodejs),陌生

[英]CommonJS Modules (with nodejs), strangeness

Okay, experimenting with CommonJS module system in the context of NodeJS. 好的,在NodeJS上下文中试验CommonJS模块系统。

module.exports = pricingCalculator;

function pricingCalculator (options) {
  var target = {};
  return target;
}

This works. 这可行。 Presumably the variable declaration of pricingCalculator is hoisted to the top of the function scope, so the misordering doesnt quite matter as the function is passed by reference anyway. 大概是PricingCalculator的变量声明被提升到函数作用域的顶部,因此,无论如何,按顺序通过引用传递函数都不会造成顺序混乱。 I get that. 我明白了。 What I dont understand is why the following two versions that work: 我不明白的是为什么以下两个版本有效:

module.exports = pricingCalculator;

var pricingCalculator = function (options) {
  var target = {};
  return target;
}

Fail. 失败。

module.exports = pricingCalculator;

pricingCalculator = function (options) {
  var target = {};
  return target;
}

Fail. 失败。 Curious to understand deeply what is going on. 好奇地了解正在发生的事情。

In first example function is defined before assignment (java script way). 在第一个示例中,函数是在赋值之前定义的(Java脚本方式)。
In second and third examples assignments are executed in sequence. 在第二和第三示例中,分配是按顺序执行的。

http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript http://studiokoi.com/blog/article/execution_order_of_functions_and_variables_in_javascript_and_actionscript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM