Last active
August 29, 2015 14:07
-
-
Save brandonvanha/7ab59bc5579050c16979 to your computer and use it in GitHub Desktop.
Defining Accessors via an Object Literal & Property Descriptors
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
// Via Object Literal | |
var obj = { | |
get foo() { | |
return 'getter'; | |
}, | |
set foo(value) { | |
console.log('setter: ' + value); | |
} | |
}; | |
// Via Property Descriptors | |
var obj = Object.create(Object.prototype, { // object with property descriptors | |
foo: { // property descriptor | |
get: function () { | |
return 'getter' | |
}, | |
enumerable: true, | |
configurable: false | |
//etc. | |
}, | |
set: function (value) { | |
console.log('setter: '+value); | |
} | |
} | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment