Skip to content

Instantly share code, notes, and snippets.

@blarfoon
Last active February 21, 2022 16:51
Show Gist options
  • Save blarfoon/e0488a6856f29374fb997ffaa2914042 to your computer and use it in GitHub Desktop.
Save blarfoon/e0488a6856f29374fb997ffaa2914042 to your computer and use it in GitHub Desktop.
const myObject = {};
Object.defineProperty(myObject, "hi", {
value: "mom",
writable: true, // default: false
enumerable: true, // default: false
configurable: true, // default: false
});
console.log(myObject.hi); // > mom
myObject.hi = "medium";
console.log(myObject.hi); // > medium
delete myObject.hi;
console.log(myObject.hi); // > undefined
Object.defineProperty(myObject, "hi", {
value: "mom",
writable: true, // default: false
enumerable: true, // default: false
configurable: true, // default: false
});
console.log(myObject.hi); // > mom
Object.defineProperty(myObject, "hello", {
value: "world",
writable: false, // default: false
enumerable: false, // default: false
configurable: false, // default: false
});
console.log(myObject.hello); // > world
myObject.hello = "medium";
console.log(myObject.hello); // > world
delete myObject.hello;
console.log(myObject.hello); // > world
// > Uncaught TypeError: Cannot redefine property: hello at Function.defineProperty (<anonymous>)
Object.defineProperty(myObject, "hello", {
value: "medium",
writable: false, // default: false
enumerable: false, // default: false
configurable: false, // default: false
});
console.log(myObject.hello);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment