File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -307,7 +307,36 @@ JavaScript不像Java或者其它语言,它没有专门的提供私有、保护
307
307
308
308
这个例子也是所谓的“模块模式”的基础,我们稍后将讲到它。
309
309
310
+ ### 原型和私有成员
310
311
312
+ 使用构造函数创建私有成员的一个弊端是,每一次调用构造函数创建对象时这些私有成员都会被创建一次。
313
+
314
+ 这对在构建函数中添加到` this ` 的成员来说是一个问题。为了避免重复劳动,节省内存,你可以将共用的属性和方法添加到构造函数的` prototype ` (原型)属性中。这样的话这些公共的部分会在使用同一个构造函数创建的所有实例中共享。你也同样可以在这些实例中共享私有成员。你可以将两种模式联合起来达到这个目的:构造函数中的私有属性和对象字面量中的私有属性。因为` prototype ` 属性也只是一个对象,可以使用对象字面量创建。
315
+
316
+ 这是一个示例:
317
+
318
+ function Gadget() {
319
+ // private member
320
+ var name = 'iPod';
321
+ // public function
322
+ this.getName = function () {
323
+ return name;
324
+ };
325
+ }
326
+
327
+ Gadget.prototype = (function () {
328
+ // private member
329
+ var browser = "Mobile Webkit";
330
+ // public prototype members
331
+ return {
332
+ getBrowser: function () {
333
+ return browser;
334
+ }
335
+ };
336
+ }());
337
+
338
+ var toy = new Gadget();
339
+ console.log(toy.getName()); // privileged "own" method console.log(toy.getBrowser()); // privileged prototype method
311
340
312
341
313
342
You can’t perform that action at this time.
0 commit comments