File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -462,7 +462,38 @@ MYAPP.utilities.array = (function () {
462
462
463
463
模块模式被广泛使用,这是一种值得强烈推荐的模式,它可以帮助组织代码,尤其是代码量在不断增长的时候。
464
464
465
+ ### 暴露模块模式
465
466
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
+ }());
466
497
467
498
468
499
You can’t perform that action at this time.
0 commit comments