开发者

CommonJS Modules (with nodejs), strangeness

Okay, experimenting with CommonJS module system in the context of NodeJS.

module.exports = pricingCalculator;

function pricingCalculator (op开发者_StackOverflow中文版tions) {
  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. 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).
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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜