Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(`\`` === "`"){
- console.log(true);
- }
- //Multi-line strings
- console.log("string text line 1 \nstring text line2");
- console.log(`string text line1
- string text line2`);
- //Expression interpolation
- var a = 5;
- var b = 10;
- console.log("Fifteen is" + ( a + b ) + " and\nnot " + (2 * a + b) + "." );
- console.log(`Fifteen is ${ a + b } and
- not ${ 2 * a + b }`);
- //Nesting templates
- function isLargeScreen(){
- return false;
- }
- var item = {'isCollapsed' : true};
- var classes = 'header ';
- classes += (isLargeScreen() ? '' : item.isCollapsed ? 'icon-expander' : ' icon-collapser');
- console.log(classes);
- const classes2 = `header ${isLargeScreen() ? '' :
- (item.isCollapsed ? 'icon-expander' : 'icon-collapser')}`;
- console.log(classes2);
- const classes3 = `header ${isLargeScreen() ? '' :
- `icon-${item.isCollapsed ? 'expander' : 'collapser'}`}`;
- console.log(classes3);
- //Tagged templates
- var person = "Mike";
- var age = 28;
- function myTag(strings, personExp, ageExp) {
- var str0 = strings[0];
- var str1 = strings[1];
- var ageStr;
- if(ageExp > 99){
- ageStr = 'centenarian';
- } else {
- ageStr = 'youngster';
- }
- return str0 + personExp + str1 + ageStr;
- }
- var output = myTag`that ${person} is a ${age}`;
- console.log(output);
- function template(strings, ...keys){
- return (function(...values){
- var dict = values[values.length - 1] || {};
- var result = [strings[0]];
- keys.forEach(function (key, i) {
- var value = Number.isInteger(key) ? values[key] : dict[key];
- result.push(value,strings[i + 1]);
- });
- return result.join('');
- });
- }
- var t1Closure = template `${0}${1}${0}!`;
- console.log(t1Closure('Y','A'));
- var t2Closure = template `${0}${'foo'}!`;
- console.log(t2Closure('Hello ',{foo : 'World'}));
- //Raw string
- function tag(strings){
- console.log(strings);
- console.log(strings.raw[0]);
- }
- tag`string text line1 \n string text line2`;
- tag`Hello World! \r Welcome to The World`;
- var str = String.raw`Hi\n${2+3}!`;
- console.log(str);
- console.log(str.length);
- console.log(str.split('').join(','));
- //Tagged templates and escape sequences
- latex`\uincode`;
- function latec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement