gpt4 book ai didi

javascript - CommonJS 模块模式

转载 作者:搜寻专家 更新时间:2023-11-01 00:30:44 27 4
gpt4 key购买 nike

这是我从

Flux architecture

var AppDispatcher = require('../dispatcher/AppDispatcher');
var EventEmitter = require('events').EventEmitter;
var TodoConstants = require('../constants/TodoConstants');
var assign = require('object-assign');

var CHANGE_EVENT = 'change';

var _todos = {}; // collection of todo items

/**
* Create a TODO item.
* @param {string} text The content of the TODO
*/
function create(text) {
// Using the current timestamp in place of a real id.
var id = Date.now();
_todos[id] = {
id: id,
complete: false,
text: text
};
}

/**
* Delete a TODO item.
* @param {string} id
*/
function destroy(id) {
delete _todos[id];
}

var TodoStore = assign({}, EventEmitter.prototype, {

/**
* Get the entire collection of TODOs.
* @return {object}
*/
getAll: function() {
return _todos;
},

emitChange: function() {
this.emit(CHANGE_EVENT);
},

/**
* @param {function} callback
*/
addChangeListener: function(callback) {
this.on(CHANGE_EVENT, callback);
},

/**
* @param {function} callback
*/
removeChangeListener: function(callback) {
this.removeListener(CHANGE_EVENT, callback);
},

dispatcherIndex: AppDispatcher.register(function(payload) {
var action = payload.action;
var text;

switch(action.actionType) {
case TodoConstants.TODO_CREATE:
text = action.text.trim();
if (text !== '') {
create(text);
TodoStore.emitChange();
}
break;

case TodoConstants.TODO_DESTROY:
destroy(action.id);
TodoStore.emitChange();
break;

// add more cases for other actionTypes, like TODO_UPDATE, etc.
}

return true; // No errors. Needed by promise in Dispatcher.
})

});

它说的地方

There are a few important things to note in the above code. To start, we are maintaining a private data structure called _todos. This object contains all the individual to-do items. Because this variable lives outside the class, but within the closure of the module, it remains private — it cannot be directly changed from outside of the module. This helps us preserve a distinct input/output interface for the flow of data by making it impossible to update the store without using an action.

粗体部分我不清楚。 js 解释器如何知道所有这些代码都在模块闭包内而不是在全局范围内?模块闭包从哪里开始,从哪里结束?

据我所知

The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.

有什么解释吗?

最佳答案

你实际上错过了你引用的摘录中的最后一行:

module.exports = TodoStore;

CommonJS 是一个 API,用于定义使用以下约定的模块:

  • 每个文件定义一个模块并在隔离环境中执行;也就是说,它定义的变量在模块外部不可用
  • 为了允许导入其他模块,模块可以使用全局变量require,允许它导入其他模块
  • 以同样的方式,变量 module 可用于模块,以便它可以设置其 exports 属性来定义模块应导出的内容;您在模块 a.js 中为 module.exports 设置的值正是 require('./a') 将返回的值。

每个实现 CommonJS 的 JS 环境都必须知道这些规则。这当然包括 Node.js,但也包括 Browserify 和 Webpack 等 bundler ,它们将打包您的代码,以便遵守这些约定。

这样,您可以控制要导出模块的哪一部分。

附言:请注意,您还可以使用 exports var 来定义导出,并且它的使用与 module.exports 略有不同。参见 this StackOverflow question and answer详情

关于javascript - CommonJS 模块模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com