Last active
November 3, 2016 11:52
-
-
Save juanmaguitar/fa284ba904992a4ece44e6e42ec81498 to your computer and use it in GitHub Desktop.
es2015 Enhanced Objects Example
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
var name = "juanma"; | |
var handler = () => `handling things for ${this.name}...`; | |
var theProtoObj = { | |
location: "barcelona", | |
toString() { | |
return `I'm super!!` | |
} | |
} | |
var obj = { | |
// __proto__ | |
__proto__: theProtoObj, | |
// Shorthand for ‘name: name’ & ‘handler: handler’ | |
name, handler, | |
// Methods | |
toString() { | |
// Super calls | |
return "everyone says " + super.toString(); | |
}, | |
// Computed (dynamic) property names | |
[ 'prop_' + (() => 42)() ]: 42 | |
}; | |
obj.toString() // "everyone says I'm super!!" | |
obj.prop_42 // 42 | |
obj.handler(); // "handling things for juanma..." | |
obj.name; // "juanma" | |
obj.location; // "barcelona" | |
//---------------------------- | |
var messages = { | |
get latest () { | |
if (this.log.length == 0) return; | |
return this.log[this.log.length - 1] | |
}, | |
set current (str) { | |
this.log[this.log.length] = str; | |
}, | |
log: [] | |
} | |
messages.current = "hey!"; | |
messages.latest // hey! | |
messages.current = "you!"; | |
messages.latest // you! | |
messages.log // ['hey!','you!'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment