Sometimes, we want to define a property of an advanced object (has setter and getter function).

Now, we could use this helper function Object.defineProperty to define property of an object in a cool way.

Example :

const foo = {};

Object.defineProperty(a, 'bar', {
value: 'hogehoge',
writable: false,

console.log(; // 'hogehoge' = 'foo bar'; // throw an error in strict mode
console.log(; // still be 'hogehoge'

Modifying setter, getter function

// Get callstack which function is getting or setting cookie value
Object.defineProperty(document, 'cookie', {
get: function() {
console.log('get !');
set: function(val) {
console.log('set = ', val);