可以用來模擬 Java 的 private 封裝特性。
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | function MyObject(name, size) {this._name = name;
 this._size = size;
 this.getSize = function() {
 return this._size;
 }
 }
 var myObject = new MyObject("Justin", 20);
 alert(myObject.getSize());
 
 | 
每次用 new 建立 MyObject 實體時,都會建立一個新的函式物件,並讓 getSize() 參考,在程式中隱含建立一個 Closure,需注意資源綁定的問題。
| 12
 3
 4
 5
 6
 7
 
 | function MyObject(name, size) {this._name = name;
 this._size = size;
 }
 MyObject.prototype.getSize = function() {
 return this._size;
 }
 
 | 
可以避免產生 Closure,而要注意的是:prototype 可以在建構式到使用 new 呼叫之間修改。建構式被呼叫前,設定給 prototype 的東西才會被物件繼承。
| 12
 3
 4
 5
 6
 
 | MyObject.prototype.color = "red";var obj1 = new MyObject();
 
 MyObject.prototype.color = "blue";
 MyObject.prototype.sound = "Ah...";
 var obj2 = new MyObject();
 
 |