Skip to content

Instantly share code, notes, and snippets.

@johnjbarton
Created August 7, 2012 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnjbarton/3287285 to your computer and use it in GitHub Desktop.
Save johnjbarton/3287285 to your computer and use it in GitHub Desktop.
quasi-literal vs function calls
// Comparing some of the use-cases for 'quasi-literals' aka string templates to normal functions and strings
// http://wiki.ecmascript.org/doku.php?id=harmony:quasis#syntax
x`foo${bar}baz`
// vs
x('foo',bar,'baz');
// http://wiki.ecmascript.org/doku.php?id=harmony:quasis#secure_content_generation
safehtml`<a href="${url}?q=${query}" onclick=alert(${message}) style="color: ${color}">${message}</a>`
// vs
safehtml('<a href="',url,'?q=',query,'" onclick=alert(', message,') style="color: ',color,'>',message,'</a>');
// http://wiki.ecmascript.org/doku.php?id=harmony:quasis#text_l10n
msg`Welcome to ${siteName}, you are visitor number ${visitorNumber}!`
// vs
msg('Welcome to ',siteName,', you are visitor number ',visitorNumber,'!');
@rauschma
Copy link

rauschma commented Aug 7, 2012

String template -> template string.

Things become more interesting once you want to escape things that are filled in. For example if url above contains double quotes ("). With your non-template-string syntax above you cannot make the distinction between, say, 'Welcome to ' and siteName. Furthermore, for HTML you can use both quotes inside a literal:

html`<a href="javascript:alert('hello')"></a>`

Not the best of examples, but you get the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment