newを使わない場合はこう。
var x = {};
(function(obj) {
var maxLength;
function getMaxLength() { return maxLength; }
function setMaxLength(val) { maxLength = val; }
obj.getMaxLength = getMaxLength;
obj.setMaxLength = setMaxLength;
})(x);
console.log(x.maxLength); // => undefined
x.setMaxLength(100);
console.log(x.getMaxLength()); // => 100
console.log(x.maxLength); // => undefined
x.maxLength = 200; // => can't change Foo's property
console.log(x.getMaxLength()); // => 100
try {
console.log(x.getMaxlength());
} catch (e) {
console.log(e.message);
}
