Created
December 10, 2015 21:10
-
-
Save robjens/deac999e469ffd07299b to your computer and use it in GitHub Desktop.
ES6 tagged template strings, this helped me grok it a bit better
This file contains hidden or 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 a = 5; | |
| var b = 10; | |
| var c = 2; | |
| function tag(strings, ...values) { | |
| console.log(strings[0].replace('\n', '')); // "Hello " | |
| console.log(strings[1].replace('\n', '')); // " world " | |
| console.log(values[0]); // 15 | |
| console.log(values[1]); // 50 | |
| return "Bazinga!"; | |
| } | |
| tag`Hello ${ a + b + c }world ${ a * b }`; | |
| // ~ tag("Hello", a + b + c) | |
| // ~ tag("world", a * b) | |
| tag`Hello ${ a + b + c } | |
| world ${ a * b }`; | |
| // now here the \n is part of the second call string, not the first | |
| tag` | |
| Hello ${ a + b + c } | |
| world ${ a * b }`; | |
| // here both are prefixed with a \n newline | |
| // now we can begin to see, these are two parameters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment