Skip to content

Commit 2297a71

Browse files
committed
模块模式 之 暴露模块模式 翻译完毕
1 parent 267b34b commit 2297a71

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

chapter5.markdown

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,38 @@ MYAPP.utilities.array = (function () {
462462

463463
模块模式被广泛使用,这是一种值得强烈推荐的模式,它可以帮助组织代码,尤其是代码量在不断增长的时候。
464464

465+
### 暴露模块模式
465466

467+
我们在本章中讨论私有成员模式时已经讨论过暴露模式。模块模式也可以用类似的方法来组织,将所有的方法保持私有,只在最后暴露需要使用的方法来初始化API。
468+
469+
上面的例子可以变成这样:
470+
471+
MYAPP.utilities.array = (function () {
472+
473+
// private properties
474+
var array_string = "[object Array]",
475+
ops = Object.prototype.toString,
476+
477+
// private methods
478+
inArray = function (haystack, needle) {
479+
for (var i = 0, max = haystack.length; i < max; i += 1) {
480+
if (haystack[i] === needle) {
481+
return i;
482+
}
483+
}
484+
return −1;
485+
},
486+
isArray = function (a) {
487+
return ops.call(a) === array_string;
488+
};
489+
// end var
490+
491+
// revealing public API
492+
return {
493+
isArray: isArray,
494+
indexOf: inArray
495+
};
496+
}());
466497

467498

468499

0 commit comments

Comments
 (0)