Skip to content

Instantly share code, notes, and snippets.

@videlais
Created May 2, 2014 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save videlais/8e4d9bc46cdf2e6ef623 to your computer and use it in GitHub Desktop.
Save videlais/8e4d9bc46cdf2e6ef623 to your computer and use it in GitHub Desktop.
JS Lesson 9
function A() {
this.value = 2;
}
//Property is explicitly set and cannot be changed directly
Object.defineProperty(A, "noValueChange", {
configurable: false, //This value cannot be changed or directly deleted
enumerable: false, //This property will not not be listed with other properties
writable: false, //This property cannot be assigned a new value
value: "NoValue" //The value
});
//Property can be read and written to (assigned a value)
Object.defineProperty(A, "changeableValue", {
get: function() {
return this.value;
},
set: function(newValue) {
this.value = newValue;
}
});
//A read-only property with a computed value
Object.defineProperty(A, "valueSquared", {
get: function() {
return this.value ^ 2; //Returns a computed value
}
});
//A write-only property with a computed value
Object.defineProperty(A, "valueMultiplied", {
set: function(value) {
this.value *= value; //Sets a computed value
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment