JavaScript 模块与正则表达式全解析
1. 模块的私有命名空间
在 JavaScript 里,模块一般会导出一个公共 API,这些 API 包含函数、类、属性和方法,供其他开发者使用。不过,模块的实现往往还需要一些额外的函数或方法,这些是不希望被模块外部调用的。比如 Set._v2s() 函数,我们不希望 Set 类的用户调用它,最好让它不可访问。
我们可以把模块(这里以 Set 类为例)定义在一个函数内部。在另一个函数内部定义的变量和函数,只在该函数内部可见,外部无法访问。实际上,我们可以把函数的作用域(有时称为“模块函数”)当作模块的私有命名空间。
以下是 Set 类在模块函数中的示例代码:
// Declare a global variable Set and assign it the return value of this function
// The open parenthesis and the function name below hint that the function
// will be invoked immediately after being defined, and that it is the function
// return value, not the function itself, that is being assigned.
// Note that this is a function expres
超级会员免费看
订阅专栏 解锁全文
7481

被折叠的 条评论
为什么被折叠?



