Created
August 8, 2021 11:09
-
-
Save marczhermo/d3c9fdbe8e017dd04c15fbde79debba6 to your computer and use it in GitHub Desktop.
Getter and Setter JS
This file contains hidden or 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
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