File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -338,5 +338,51 @@ JavaScript不像Java或者其它语言,它没有专门的提供私有、保护
338
338
var toy = new Gadget();
339
339
console.log(toy.getName()); // privileged "own" method console.log(toy.getBrowser()); // privileged prototype method
340
340
341
+ ### 将私有函数暴露为公有方法
341
342
343
+ “暴露模式”是指将已经有的私有函数暴露为公有方法。当对对象进行操作时,所有功能代码都对这些操作很敏感,而你想尽量保护这些代码的时候很有用。(译注:指对来自外部的修改很敏感。)但同时,你又希望能提供一些功能的访问权限,因为它们会被用到。如果你把这些方法公开,就会使得它们不再健壮,你的API的使用者可能修改它们。在ECMAScript5中,你可以选择冻结一个对象,但在之前的版本中不可用。下面进入暴露模式(原来是由Christian Heilmann创造的模式,叫“暴露模块模式”)。
344
+
345
+ 我们来看一个例子,它建立在对象字面量的私有成员模式之上:
346
+
347
+ var myarray;
348
+
349
+ (function () {
350
+
351
+ var astr = "[object Array]",
352
+ toString = Object.prototype.toString;
353
+
354
+ function isArray(a) {
355
+ return toString.call(a) === astr;
356
+ }
357
+
358
+ function indexOf(haystack, needle) {
359
+ var i = 0,
360
+ max = haystack.length;
361
+ for (; i < max; i += 1) {
362
+ if (haystack[i] === needle) {
363
+ return i;
364
+ }
365
+ }
366
+ return −1;
367
+ }
368
+
369
+ myarray = {
370
+ isArray: isArray,
371
+ indexOf: indexOf,
372
+ inArray: indexOf
373
+ };
374
+
375
+ }());
376
+
377
+ 这里有两个私有变量和两个私有函数——` isArray() ` 和` indexOf() ` 。在包裹函数的最后,使用那些允许被从外部访问的函数填充` myarray ` 对象。在这个例子中,同一个私有函数 ` indexOf() ` 同时被暴露为ECMAScript 5风格的` indexOf ` 和PHP风格的` inArry ` 。测试一下myarray对象:
378
+
379
+ myarray.isArray([1,2]); // true
380
+ myarray.isArray({0: 1}); // false
381
+ myarray.indexOf(["a", "b", "z"], "z"); // 2
382
+ myarray.inArray(["a", "b", "z"], "z"); // 2
383
+
384
+ 现在假如有一些意外的情况发生在暴露的` indexOf() ` 方法上,私有的` indexOf() ` 方法仍然是安全的,因此` inArray() ` 仍然可以正常工作:
385
+
386
+ myarray.indexOf = null;
387
+ myarray.inArray(["a", "b", "z"], "z"); // 2
342
388
You can’t perform that action at this time.
0 commit comments