Java Reference
In-Depth Information
Object.defineProperty(mike, 'color', { value: "orange",
writable: false, enumerable: true });
// configurable will be false by default
<< {"attack": function (){
return + " hits you with his " + this.weapon;
}, "color": "orange", "eat": function () {
return "Mmm, this " + + " tastes great!";
}, "food": "Pizza", "name": "Michelangelo", "sayHi":
function () {
return "Hi dude, my name is " +;
}, "weapon": "Nunchuks"}
As you can see, the object is returned with the the new property added.
Getters and Setters
Every object property descriptor can also have get() and set() methods, which can be
used to control how a property is set using assignment and which value is returned. For
example, if we create an empty object and then add a new property to it, we can include
get() and set() methods like so:
example = {}; // initialize an empty object literal
Object.defineProperty(example, 'sillyString', {
get: function() {
return "Craaazy!";
set: function(value) {
return value;
To test this out, we can assign a string to the sillyString property:
example.sillyString = "Hello";
<< "Hello"
Search WWH ::

Custom Search