tagged-templates

ES6 tagged templates practical usability

两盒软妹~` 提交于 2019-12-28 03:04:10
问题 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? 回答1: You can use tagged templates to build

ES6 calling function with template literal but no parentheses [duplicate]

六眼飞鱼酱① 提交于 2019-12-19 17:35:15
问题 This question already has answers here : Backticks calling a function (2 answers) Closed 2 years ago . According to MDN, Tagged template literals can be used as follows: var a = 5; var b = 10; function tag(strings, ...values) { alert(strings[0]); // "Hello " alert(strings[1]); // " world " alert(values[0]); // 15 alert(values[1]); // 50 return "Bazinga!"; } tag `Hello ${ a + b } world ${ a * b }`; // "Bazinga!" In the example above, the function tag is called without using parentheses. I

ES6 calling function with template literal but no parentheses [duplicate]

一曲冷凌霜 提交于 2019-12-19 17:34:31
问题 This question already has answers here : Backticks calling a function (2 answers) Closed 2 years ago . According to MDN, Tagged template literals can be used as follows: var a = 5; var b = 10; function tag(strings, ...values) { alert(strings[0]); // "Hello " alert(strings[1]); // " world " alert(values[0]); // 15 alert(values[1]); // 50 return "Bazinga!"; } tag `Hello ${ a + b } world ${ a * b }`; // "Bazinga!" In the example above, the function tag is called without using parentheses. I

ES6 calling function with template literal but no parentheses [duplicate]

佐手、 提交于 2019-12-01 16:36:54
This question already has an answer here: Backticks calling a function 2 answers According to MDN, Tagged template literals can be used as follows: var a = 5; var b = 10; function tag(strings, ...values) { alert(strings[0]); // "Hello " alert(strings[1]); // " world " alert(values[0]); // 15 alert(values[1]); // 50 return "Bazinga!"; } tag `Hello ${ a + b } world ${ a * b }`; // "Bazinga!" In the example above, the function tag is called without using parentheses. I expected it should be called like tag(`Hello`) , but that passes the string resulting from the template literal as the argument

Javascript Es6 Tagged Templates - When is raw used? When is cooked used?

不问归期 提交于 2019-12-01 06:25:29
After studying this Es6 tag template example: var yo = func`${x} + ${y}\n= ${x + y}`; one@public-node ~/es6 $ 6to5 tag.js "use strict"; var _taggedTemplateLiteral = function (strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }; var yo = func(_taggedTemplateLiteral(["", " + ", "\n= ", ""], ["", " + ", "\\n= ", ""]), x, y, x + y); I see what is returned is var yo = func(strings, raw, x, y, x + y); I understand the basics about the string literals and the x y values being inserted. What I don't understand is...when is strings used

Javascript Es6 Tagged Templates - When is raw used? When is cooked used?

六月ゝ 毕业季﹏ 提交于 2019-12-01 05:32:18
问题 After studying this Es6 tag template example: var yo = func`${x} + ${y}\n= ${x + y}`; one@public-node ~/es6 $ 6to5 tag.js "use strict"; var _taggedTemplateLiteral = function (strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }; var yo = func(_taggedTemplateLiteral(["", " + ", "\n= ", ""], ["", " + ", "\\n= ", ""]), x, y, x + y); I see what is returned is var yo = func(strings, raw, x, y, x + y); I understand the basics about the

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

Backticks calling a function

巧了我就是萌 提交于 2019-11-25 23:32:51
问题 I\'m not sure how to explain this, but when I run console.log`1` In google chrome, I get output like console.log`1` VM12380:2 [\"1\", raw: Array[1]] Why is the backtick calling the log function, and why is it making a index of raw: Array[1] ? Question brought up in the JS room by Catgocat, but no answers made sense besides something about templating strings that didn\'t really fit why this is happening. 回答1: It is called Tagged Template in ES-6 more could be read about them Here, funny I