template-strings

Is it possible to have a comment inside a es6 Template-String?

一曲冷凌霜 提交于 2019-12-01 03:05:31
Let's say we have a multiline es6 Template-String to describe e.g. some URL params for a request: const fields = ` id, message, created_time, permalink_url, type `; Is there any way to have comments inside that backtick Template-String? Like: const fields = ` // post id id, // post status/message message, // ..... created_time, permalink_url, type `; No. That syntax is valid, but will just return a string containing \n// post id\nid , rather than removing the comments and creating a string without them. If you look at §11.8.6 of the spec , you can see that the only token recognized between the

Template Strings ES6 prevent line breaks

泄露秘密 提交于 2019-11-30 12:23:58
问题 I have a long string, which I build using ES6 template strings, but I want it to be without line breaks: var string = `As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names. For example, below we can use expression interpolation to embed for some readable inline math:` console.log(string); Result: As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names.

Multiline strings that don't break indentation

那年仲夏 提交于 2019-11-30 10:50:44
According to this esdiscuss discussion , it is possible in ECMAScript 6 to define multiline strings without having to place subsequent lines of the string at the very beginning of the line. Allen Wirfs-Brock’s post contains a code example: var a = dontIndent `This is a template string. Even though each line is indented to keep the code neat and tidy, the white space used to indent is not in the resulting string`; Could someone explain how this can be achieved? How to define this dontIndent thing in order to remove the whitespace used for indentation? This feature is implemented by defining a

Why can functions be called without parentheses when using template strings? [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-30 04:42:22
This question already has an answer here: Backticks calling a function 2 answers I have a simple logging function: function log(str) { console.log('logged: ', str); } If I call it without parentheses (currently using Chrome's dev tools) and pass in a template string, like this: log`foo` The output is: logged: ["foo", raw: Array[1]] If I call it with parentheses, log(`foo`) The output is: logged: foo Why does calling a function using a template string an no parentheses work in Javascript? What is happening that causes the result to be different from calling it with parentheses? Sampson The

Template Strings ES6 prevent line breaks

独自空忆成欢 提交于 2019-11-30 02:40:36
I have a long string, which I build using ES6 template strings, but I want it to be without line breaks: var string = `As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names. For example, below we can use expression interpolation to embed for some readable inline math:` console.log(string); Result: As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names. For example, below we can use expression interpolation to embed for some readable inline math: My

Multiline strings that don't break indentation

我只是一个虾纸丫 提交于 2019-11-29 15:57:34
问题 According to this esdiscuss discussion, it is possible in ECMAScript 6 to define multiline strings without having to place subsequent lines of the string at the very beginning of the line. Allen Wirfs-Brock’s post contains a code example: var a = dontIndent `This is a template string. Even though each line is indented to keep the code neat and tidy, the white space used to indent is not in the resulting string`; Could someone explain how this can be achieved? How to define this dontIndent

Why can functions be called without parentheses when using template strings? [duplicate]

自作多情 提交于 2019-11-29 02:03:32
问题 This question already has answers here : Backticks calling a function (2 answers) Closed 2 years ago . I have a simple logging function: function log(str) { console.log('logged: ', str); } If I call it without parentheses (currently using Chrome's dev tools) and pass in a template string, like this: log`foo` The output is: logged: ["foo", raw: Array[1]] If I call it with parentheses, log(`foo`) The output is: logged: foo Why does calling a function using a template string an no parentheses

Can you “dumb down” ES6 template strings to normal strings?

▼魔方 西西 提交于 2019-11-28 01:52:33
I have to work around the limitation of gettext to recognise ES6 template strings, and I thought about getting the "non interpolated value" of the template strings as a compilation step, in order to have only "normal" strings in the code. Basically what I would like to achieve is transform this const adjective = 'wonderful' const something = `Look, I am a ${adjective} string` console.log(something) > "Look, I am a wonderful string" into this const adjective = 'wonderful' const something = 'Look, I am a ${adjective} string' console.log(something) > "Look, I am a ${adjective} string" One brutal

What are the actual uses of ES6 Raw String Access?

痞子三分冷 提交于 2019-11-27 17:12:19
问题 What are the actual uses of String.raw Raw String Access introduced in ECMAScript 6? // String.raw(callSite, ...substitutions) function quux (strings, ...values) { strings[0] === "foo\n" strings[1] === "bar" strings.raw[0] === "foo\\n" strings.raw[1] === "bar" values[0] === 42 } quux `foo\n${ 42 }bar` String.raw `foo\n${ 42 }bar` === "foo\\n42bar" I went through the below docs. http://es6-features.org/#RawStringAccess https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template

ES6 tagged templates practical usability

醉酒当歌 提交于 2019-11-27 09:04:52
I understand the syntax of ES6 tagged templates . What I don't see is the practical usability. When is it better than passing an object parameter, like the settings in jQuery's AJAX ? $.ajax('url', { /*this guy here*/ }) Right now I only see the tricky syntax but I don't see why I would need/use it. I also found that the TypeScript team chose to implement it (in 1.5) before other important features. What is the concept behind tagged string templates? You can use tagged templates to build APIs that are more expressive than regular function calls. For example, I'm working on a proof-of-concept