为什么这段代码不起作用?

asd*_*qwe 0 javascript scope iife

我在这里嘲笑了一些代码

var common = common || {};

(function(NAMESPACE) {

  NAMESPACE = {
    isIE: function() {
        return true;
    }
  };

  main();

})(common);

function main() {
    console.log(common.isIE());
  return 'Hello, World!';
}
Run Code Online (Sandbox Code Playgroud)

我想了解一些事情,

1)为什么这不起作用,我想这与确定范围如何"决定"和IIFE有关,但并不完全确定.

2)如何使这个代码工作?

Jag*_*row 5

common作为参数传递的名称NAMESPACE需要扩展而不是分配新值.

所以Object.assign可以在这里帮忙.

var common = common|| {};

(function(NAMESPACE) {

  Object.assign(NAMESPACE,{
    isIE: function() {
        return true;
    }
  });

  main();

})(common);

function main() {
    console.log(common.isIE());
  return 'Hello, World!';
}
Run Code Online (Sandbox Code Playgroud)