-
-
Save videlais/8e4d9bc46cdf2e6ef623 to your computer and use it in GitHub Desktop.
JS Lesson 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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