Comments on: Straight-up interpolation https://j11y.io/javascript/straight-up-interpolation/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Alexander https://j11y.io/javascript/straight-up-interpolation/#comment-2667 Thu, 28 Nov 2013 20:21:35 +0000 https://j11y.io/?p=2364#comment-2667 Changing replace(/{([sS]+?)}/g, ‘” + o[“$1”] + “‘) to replace(/{([sS]+?)}/g, ‘” + o[“$1″] || ” + “‘) would prevent ‘undefined’ from showing up in the template.

]]>
By: James https://j11y.io/javascript/straight-up-interpolation/#comment-2666 Thu, 15 Aug 2013 09:12:41 +0000 https://j11y.io/?p=2364#comment-2666 @Mathias, good to know — I never realised ES5 was so liberal with reserved words.

]]>
By: Mathias Bynens https://j11y.io/javascript/straight-up-interpolation/#comment-2665 Tue, 13 Aug 2013 10:44:53 +0000 https://j11y.io/?p=2364#comment-2665 Just a quick note:

It uses square-bracket notation to access the property values instead of the dot notation just in-case someone ends up using a reserved-word (e.g. `instanceof`) or an invalid JS identifier.

Note that this is only needed if backwards compatibility with ES3 engines is a concern. In ES5, this is perfectly valid code:

var foo = {
  if: 42
};
console.log(foo.if); // logs `42`

That said, even if you only care about modern engines, using square brackets is still preferable here, in case the template string contains property names that aren’t numeric literals or valid identifier names.

]]>