Skip to content

Instantly share code, notes, and snippets.

@marczhermo
Created August 8, 2021 11:09
Show Gist options
  • Save marczhermo/d3c9fdbe8e017dd04c15fbde79debba6 to your computer and use it in GitHub Desktop.
Save marczhermo/d3c9fdbe8e017dd04c15fbde79debba6 to your computer and use it in GitHub Desktop.
Getter and Setter JS
const self = {
title: 'New Zealand',
hello() {
this.world();
},
world() {
console.log(`World: ${this.title}`);
},
};
const root = {};
// Creating own *this* property to the object
Object.defineProperty(root, 'this', {
get() {
console.log('getterSELF');
return self;
},
enumerable: true,
configurable: true,
});
export default Object.create(root.this, {
siteTitle: {
get() { console.log('getterTITLE'); return root.this.title; },
set(value) { console.log('setterTITLE'); root.this.title = value; }
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment