File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -495,5 +495,50 @@ MYAPP.utilities.array = (function () {
495
495
};
496
496
}());
497
497
498
+ ### 创建构造函数的模块
498
499
500
+ 前面的例子创建了一个对象` MYAPP.utilities.array ` ,但有时候使用构造函数来创建对象会更方便。你也可以同样使用模块模式来做。唯一的区别是包裹模块的立即执行的函数会在最后返回一个函数,而不是一个对象。
501
+
502
+ 看下面的模块模式的例子,创建了一个构造函数` MYAPP.utilities.Array ` :
503
+
504
+ MYAPP.namespace('MYAPP.utilities.Array');
505
+
506
+ MYAPP.utilities.Array = (function () {
507
+
508
+ // dependencies
509
+ var uobj = MYAPP.utilities.object,
510
+ ulang = MYAPP.utilities.lang,
511
+
512
+ // private properties and methods...
513
+ Constr;
514
+
515
+ // end var
516
+
517
+ // optionally one-time init procedures
518
+ // ...
519
+
520
+ // public API -- constructor
521
+ Constr = function (o) {
522
+ this.elements = this.toArray(o);
523
+ };
524
+ // public API -- prototype
525
+ Constr.prototype = {
526
+ constructor: MYAPP.utilities.Array,
527
+ version: "2.0",
528
+ toArray: function (obj) {
529
+ for (var i = 0, a = [], len = obj.length; i < len; i += 1) {
530
+ a[i] = obj[i];
531
+ }
532
+ return a;
533
+ }
534
+ };
535
+
536
+ // return the constructor
537
+ // to be assigned to the new namespace return Constr;
538
+
539
+ }());
540
+
541
+ 像这样使用这个新的构造函数:
542
+
543
+ var arr = new MYAPP.utilities.Array(obj);
499
544
You can’t perform that action at this time.
0 commit comments