Skip to content

Instantly share code, notes, and snippets.

@nebrius
Last active August 29, 2015 14:07
Show Gist options
  • Save nebrius/3ac46763c079c4fd8d2b to your computer and use it in GitHub Desktop.
Save nebrius/3ac46763c079c4fd8d2b to your computer and use it in GitHub Desktop.
The Lesser Known Features of ECMAScript 6 Code
<!--
The MIT License (MIT)
Copyright (c) 2014 Bryan Hughes <[email protected]> (http://theoreticalideations.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!DOCTYPE html>
<html>
...
<body>
<h1 id="message"></h1>
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script>
traceur.options.experimental = true;
</script>
<script type="module">
console.log('Block Scoped Variables');
(function() {
{
let foo = 1;
const bar = 2;
console.log(foo, bar);
}
try { console.log(foo); } catch(e) {} // Throws an exception
})();
console.log('\nTemplate Literals');
(function() {
let foo = 'Bar';
console.log(`Hello "${foo}"`);
})();
console.log('\nComputed Property Names');
(function() {
let myProp = 'foo';
let myObj = {
[myProp]: 'bar'
};
console.log(myObj.foo); // Prints "bar"
})();
console.log('\nShorthand Properties');
(function() {
let x = 1, y = 2;
let myObj = { x, y };
console.log(Object.keys(myObj));
console.log(myObj.x, myObj.y);
})();
console.log('\nShorthand Functions');
(function() {
let myObj = {
foo() { console.log('bar'); }
}
myObj.foo();
})();
console.log('\nSymbols');
(function() {
let foo = Symbol();
let myObj = {};
myObj[foo] = 'bar';
console.log(myObj.foo, myObj[foo]);
})();
console.log('\nClasses w/ symbols');
(function() {
let bar = Symbol();
class foo {
[bar]() {
console.log('baz');
}
}
let myFoo = new foo();
myFoo[bar](); // Prints "baz"
})();
console.log('\nFat Arrow Functions');
(function() {
let myObj = {
foo: 'bar',
print() {
(() => { console.log(this.foo); })();
}
};
myObj.print();
})();
console.log('\n...rest Parameters');
(function() {
myFunc(1, 2, 3, 4, 5);
function myFunc(a, b, ...rest) {
console.log(a, b); // Prints "1, 2"
console.log(Array.isArray(rest)); // Prints "true"
console.log(rest); // Prints "[3, 4, 5]"
}
})();
console.log('\n...spread Parameters');
(function() {
let myArray = [2, 3, 4];
myFunc(1, ...myArray);
function myFunc(a, b, c, d) {
console.log(a, b, c, d); // Prints "1, 2, 3, 4"
}
})();
console.log('\nFor-Of Statements');
(function() {
let myArray = ['a', 'b', 'c'];
// Prints:
// "a"
// "b"
// "c"
for (let myElement of myArray) {
console.log(myElement);
}
})();
console.log('\nDestructuring Arrays');
(function() {
let myArray = [1, 2];
let [a, b] = myArray;
console.log(a, b); // Prints "1, 2"
})();
console.log('\nDestructuring Objects');
(function() {
let myObj = {
one: 1,
two: 2
};
let { one: a, two: b} = myObj;
console.log(a, b); // Prints "1, 2"
})();
console.log('\nMixing it up');
(function() {
let a = 'first', b = 'last';
print({
[a](c) { return `${c ? "A" : "a"}lice` },
[b](c) { return `${c ? "J" : "j"}ones` }
}, {
[a](c) { return `${c ? "B" : "b"}ob` },
[b](c) { return `${c ? "S" : "s"}mith` }
});
// Prints:
// "Jones, alice"
// "Smith, bob"
function print(...people) {
for (let { first: a, last: b } of people) {
console.log(b(true) + ', ' + a(false));
}
}
})(); // 341 characters
console.log('\nMixing it up, oldschool');
(function() {
var a = 'first', b = 'last';
var myFirstObj = {};
myFirstObj[a] = function(c) { return (c ? 'A' : 'a') + 'lice'; };
myFirstObj[b] = function(c) { return (c ? 'J' : 'j') + 'ones'; };
var mySecondObj = {};
mySecondObj[a] = function(c) { return (c ? 'B' : 'b') + 'ob'; };
mySecondObj[b] = function(c) { return (c ? 'S' : 's') + 'mith'; };
print(myFirstObj, mySecondObj);
// Prints:
// "Jones, alice"
// "Smith, bob"
function print() {
var args = Array.prototype.slice.apply(arguments);
args.forEach(function(arg) {
var a = arg.first;
var b = arg.last;
console.log(b(true) + ', ' + a(false));
});
}
})(); // 567 characters
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment