我在使用 webpack 和 chrome 的 ES6 中遇到使用范围问题
这是我的代码:
'use strict'
import Reflux from 'reflux'
import Immutable from 'immutable'
import Something from 'something'
import Cursor from 'immutable/contrib/cursor'
// everything is working in this scope
export default Reflux.createStore({
init: function() {
console.log(Something) // Something Not defined
console.log(Cursor) //Cursor Not defined
console.log(Reflux) // Reflux Not defined
},
... some other code
})
所以问题是,为什么事情是在上面的范围内定义的,而不是在 init 函数的范围内?谢谢您的帮助
babel 之后编译的代码就像“use strict”;
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
var Reflux = _interopRequire(require("reflux"));
var Immutable = _interopRequire(require("immutable"));
var Something = _interopRequire(require("something"));
var Cursor = _interopRequire(require("immutable/contrib/cursor"));
// everything is working in this scope
module.exports = Reflux.createStore({
init: function init() {
console.log(Something); // Something Not defined
console.log(Cursor); //Cursor Not defined
console.log(Reflux);
} });