Skip to content

Instantly share code, notes, and snippets.

@pha-ias
Last active August 23, 2016 19:22
Show Gist options
  • Save pha-ias/57dd71eaeb6d2e43e531e02617129f9a to your computer and use it in GitHub Desktop.
Save pha-ias/57dd71eaeb6d2e43e531e02617129f9a to your computer and use it in GitHub Desktop.
// ES6 and beyond
// -------------------------
// String Literal
// -------------------------
var someString = `Something`
typeof someString
// Multiline support
var someText =
`
Something really
long
and not interesting
`
var desc = "awesome"
// Exception when declaring a string?!
var someText =
`
Something ${awesome}
long
and not interesting
`;
// VM446:3 Uncaught ReferenceError: awesome is not defined(…)(anonymous function) @ VM446:3
var someText =
`
Something ${desc}
long
and not interesting
`
;
var someText =
`
Something ${desc.toUpperCase()}
long
and not interesting
`
;
someText
function upper(str) { return str.toUpperCase(); }
var someText =
`
Something ${upper(desc)}
long
and not interesting
`
;
someText
desc = "sucky";
someText
var someGlobal = 'some global';
function getGlobal() { return someGlobal; }
var someText =
`
Something ${getGlobal()}
long
and not interesting
`
;
someText
someGlobal = 'still a global'
getGlobal()
someText
function foo(str) {
var name = 'foo';
console.log(str)
}
function bar() {
var name = 'bar';
foo(`Hello from ${name}`);
}
var name = 'initial';
bar()
var someText = `Some text has: ${upper(`${desc}`)}`;
// -------------------------
// Tagged String Literal
// -------------------------
function foo(strings, ...values) {
console.log( strings );
console.log( values );
}
var desc = "awesome";
foo`Everything is ${desc}!`;
foo `Everything is ${desc}!`;
foo `Everything is ${desc}!`;
foo`Everything is ${desc} and ${desc2}!`;
foo(`Everything is ${desc} and ${desc2}!`); // function?
foo`${desc} and ${desc2}`;
String.raw`Hello`
String.raw`Hello` === "Hello"
String.raw`Hello\n\n${desc}`
String.raw`
Hello\n\n${desc}`
// -------------------------
// Arrow Functions
// -------------------------
var neg = x => -x
neg(234)
[1, 3, 5]
a = [2, 4, 6, 8, 10]
a.filter( e => e % 2)
a = a.map( v => v * 2 );
a = a.map( v => { return v * 2 });
// [8, 16, 24, 32, 40]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment