template-literals

Template literals syntax is not working in IE11

我们两清 提交于 2019-11-27 14:37:56
The back-tick character is not recognized as a Valid Character in IE11 when using the "use strict" directive while it works in other browsers, such as Chrome. What is the interpretation of this behavior taking into consideration that IE11 is still widely used even among Windows 10 users?? "use strict"; function doIt() { let tt; tt = 50; alert(`${tt}`); alert("test"); } doIt(); Error: { "message": "Invalid character", "filename": " http://stacksnippets.net/js ", "lineno": 18, "colno": 17 } If you look at the ECMAScript 6 compatibility table , you'll see that template literals are not supported

Wrap long template literal line to multiline without creating a new line in the string

邮差的信 提交于 2019-11-27 05:27:36
问题 In es6 template literals, how can one wrap a long template literal to multiline without creating a new line in the string? For example, if you do this: const text = `a very long string that just continues and continues and continues` Then it will create a new line symbol to the string, as interpreting it to have a new line. How can one wrap the long template literal to multiple lines without creating the newline? 回答1: If you introduce a line continuation ( \ ) at the point of the newline in

Is there a downside to using ES6 template literals syntax without a templated expression?

北城以北 提交于 2019-11-26 23:03:20
Is there a reason (performance or other) not to use backtick template literal syntax for all strings in a javascript source file? If so, what? Should I prefer this: var str1 = 'this is a string'; over this? var str2 = `this is another string`; The most significant reason not to use them is that ES6 is not supported in all environments. Of course that might not affect you at all, but still: YAGNI . Don't use template literals unless you need interpolation, multiline literals, or unescaped quotes and apostrophes. Much of the arguments from When to use double or single quotes in JavaScript? carry

Template String As Object Property Name

社会主义新天地 提交于 2019-11-26 17:35:46
Why does JavaScript not allow a template string as an object property key? For example, when I input: foo = {`bar`: 'baz'} into the NodeJS REPL, it throws a SyntaxError with "Unexpected template string" with a long stack trace. Property values are fine, however, which is not as unexpected. Similar errors happen in the browser, for example, Firebug throws a SyntaxError with "invalid property id". Template strings are allowed in "computed property names". For instance, this compiles perfectly fine in all browsers that support the syntax: var foo = { [`bar` + 1]: `baz` }; and creates the object {

Template literal inside of the RegEx

混江龙づ霸主 提交于 2019-11-26 11:39:14
问题 I tried to place a template literal inside of a RegEx, and it didn\'t work. I then made a variable regex which holds my RegEx, but it still not giving me the desired result. However if I console.log(regex) individually, I do receive the desired RegEx, such as /.+?(?=location)/i , /.+?(?=date)/i and so on, but once I place regex inside the .replace it appears not to be working function validate (data) { let testArr = Object.keys(data); errorMessages.forEach((elem, i) => { const regex = `/.+?(?

Defer execution for ES6 Template Literals

狂风中的少年 提交于 2019-11-26 11:19:16
I am playing with the new ES6 Template Literals feature and the first thing that came to my head was a String.format for JavaScript so I went about implementing a prototype: String.prototype.format = function() { var self = this; arguments.forEach(function(val,idx) { self["p"+idx] = val; }); return this.toString(); }; console.log(`Hello, ${p0}. This is a ${p1}`.format("world", "test")); ES6Fiddle However, the Template Literal is evaluated before it's passed to my prototype method. Is there any way I can write the above code to defer the result until after I have dynamically created the

What does this `…${…}…` code in the node docs mean? [duplicate]

蹲街弑〆低调 提交于 2019-11-26 10:01:04
问题 This question already has an answer here: Usage of the backtick character (`) in JavaScript? 7 answers I am to trying to learn Express library and Node.js one step at a time. First I am looking at is the specifics of the Node reqiure(moduleName) function. I took a look at the documentation for this, and found some weird code in the example documentation: const circle = require(\'./circle.js\'); console.log( `The area of a circle of radius 4 is ${circle.area(4)}`); More specifically the $

Is there a downside to using ES6 template literals syntax without a templated expression?

偶尔善良 提交于 2019-11-26 08:35:00
问题 Is there a reason (performance or other) not to use backtick template literal syntax for all strings in a javascript source file? If so, what? Should I prefer this: var str1 = \'this is a string\'; over this? var str2 = `this is another string`; 回答1: The most significant reason not to use them is that ES6 is not supported in all environments. Of course that might not affect you at all, but still: YAGNI. Don't use template literals unless you need interpolation, multiline literals, or

Template String As Object Property Name

天大地大妈咪最大 提交于 2019-11-26 04:24:28
问题 Why does JavaScript not allow a template string as an object property key? For example, when I input: foo = {`bar`: \'baz\'} into the NodeJS REPL, it throws a SyntaxError with \"Unexpected template string\" with a long stack trace. Property values are fine, however, which is not as unexpected. Similar errors happen in the browser, for example, Firebug throws a SyntaxError with \"invalid property id\". Template strings are allowed in \"computed property names\". For instance, this compiles

Defer execution for ES6 Template Literals

核能气质少年 提交于 2019-11-26 00:59:40
问题 I am playing with the new ES6 Template Literals feature and the first thing that came to my head was a String.format for JavaScript so I went about implementing a prototype: String.prototype.format = function() { var self = this; arguments.forEach(function(val,idx) { self[\"p\"+idx] = val; }); return this.toString(); }; console.log(`Hello, ${p0}. This is a ${p1}`.format(\"world\", \"test\")); ES6Fiddle However, the Template Literal is evaluated before it\'s passed to my prototype method. Is