This Gist was automatically created by Carbide, a free online programming environment.
Created
January 4, 2019 18:48
-
-
Save carbide-public/464a5139b19ae01d3d661e4177d45926 to your computer and use it in GitHub Desktop.
ES2015 Enhanced Object Literals
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
/// **Sources:** | |
/// * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer | |
/// * http://www.benmvp.com/learning-es6-enhanced-object-literals/ | |
/// * http://2ality.com/2016/01/private-data-classes.html | |
/// * https://ariya.io/2013/02/es6-and-object-literal-property-value-shorthand |
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
const NAME = "name", | |
obj = { | |
[NAME]: "John Smith" ///We're using a computed key here -- the end result will be that obj.name = John Smith. | |
}; | |
obj; | |
function namePlus(s) { | |
return s + "name"; | |
} | |
const anotherObj = { | |
["last" + "name"]: "Smith", | |
[namePlus("first")]: "John" | |
}; | |
anotherObj; | |
function createLogger() { | |
const _el = Symbol("el"), | |
el = document.createElement("div"); | |
el.style.whiteSpace = "pre-line"; | |
el.style.fontFamily = "monospace"; | |
return { | |
[_el]: el, ///We can use "computed keys", too. Here we're faking a private property. These aren't _truly_ private, but "Object.keys", for example, can't see it. ("Reflect.ownKeys", though, can.) | |
get el() { | |
return this[_el]; | |
}, | |
write(...args) { | |
this[_el].textContent += args.join(", ") + String.fromCharCode(13, 10); | |
} | |
} | |
} | |
const aDiv = createLogger(); | |
aDiv.el; | |
aDiv.write("Hello, World!"); | |
aDiv.write(".... more logging!", "... and even more!"); | |
const propKeys = Object.keys(aDiv); | |
propKeys; | |
// you'll have to take my word here, Carbide doesn't like this, but ///This isn't the point of this playground, but as you can see here (if Carbide would let us), the symbol is not hidden from Reflect.ownKeys. If you run this in the JS console on browser that supports ES2015, this will render the output listed. | |
// it works in devtools. | |
// const ownKeys = Reflect.ownKeys(aDiv); | |
// ownKeys; | |
// ["el", "write", Symbol(el)] <-- NOT PRIVATE! |
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
function createGeolocationOptions({timeout = 30000, maximumAge = 30000, highAccuracy = true} = {}) { | |
return { | |
timeout, | |
maximumAge, | |
enableHighAccuracy: highAccuracy ///It's important to remember that we can mix and match as desired. | |
}; | |
} | |
const opts = createGeolocationOptions({timeout: 5000, highAccuracy: false}); | |
opts; |
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
const x = 10, y = 20, | |
point = {x, y}; ///If our variable and the corresponding property key matches, we don't have to duplicate ourselves. ES5 would have required {x: x, y: y}. | |
po | |
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
const MATH = { | |
add(a, b) { ///You can define functions _without_ the "function" keyword. | |
return a + b; | |
}, | |
sub: (a, b) => a - b, ///NOT arrow functions -- they're anonymous, so we have to provide a key. | |
async theAnswer() { ///But we can use shorthand with Async functions... | |
return 42; | |
}, | |
* tenInts() { ///... and generators, too. | |
for (let i = 0; i < 10; i++) { | |
yield i; | |
} | |
} | |
} | |
const sum = MATH.add(2, 2); | |
sum; | |
const dif = MATH.sub(4, 2); | |
dif; | |
const theAnswer = await MATH.theAnswer(); | |
theAnswer; | |
const tenInts = [...MATH.tenInts()]; | |
tenInts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment