Object.defineProperty
It's very easy to define properties of an object in JavaScript.
var foo = {
bar: 1
};
That's it. And it's very easy to change the property's value or type. foo.bar = "a string"; which is good for flexbility, and that's why we love JavaScript.
Now, what if we make bar a readonly member? Or even if we want more power to use set/get function on the object, which is pretty easy with C++, JAVA, C#.
An ES5 function Object.defineProperty() can kindof help. This function can define a new property on an object, or modifies an existing property on an object. Not like the very stright forward old style way, Object.defineProperty() supports more access controls on the property.
Object.defineProperty() has 3 parameters: the target object, the property name, and the descriptor. The descriptor itself is a simple object, has a few of properties, not only defines the property value, but also how the property could be accessed.
the descriptor
There are 2 types of properties at the descriptor object: 1. the value, 2. access right.
To define a property's value, we can use either a value property, or set/get properties of the descriptor.
value is
