使用ejs和multiline 實現ES6 Template String的功能
es6TemplateString
使用ejs和multiline 實現ES6 Template String的功能
ES6 Template String 支持的功能
//Basic usage with an expression placeholder var person = 'Hao Ju Zheng'; console.log(`Yo! My name is ${person}`); //Expressions work just as well with object literals var user = {name: 'Zhen Ou Yun'}; console.log(`Thanks, ${user.name}`); //Expression interpolation, One use is readable inline math. var a = 50; var b = 100; console.log(`The number of JS frameworks is ${a + b} and not ${ 2 * a +b }`); //Multi-line strings without needing \n\ console.log(`string text line 1 string text line 2`); //Function inside expressions function fn() {return "result"} console.log(`foo ${fn()} bar`);
瀏覽器支持情況
- firefox 34及其以上版本
- Chrome 馬上會支持
詳見ES6兼容表
使用ejs和multiline的實現的es6TemplateString
//Basic usage with an expression placeholder var person = "Hao Ju Zheng"; console.log(es6TemplateString(function(){/*! Yo My name is ${= person } */}, {'person': person})); //Expressions work just as well with object literals var user = {name: "Zhen Ou Yun"}; console.log(es6TemplateString(function(){/*! Thanks, ${= name } */}, user)); //Multi-line strings without needing \n\ console.log(es6TemplateString(function(){/*! string text line 1 string text line 2 */}, {})); //Function inside expressions function fn() {return "result"} console.log(es6TemplateString(function(){/*! foo ${= fn() } bar */}, {'fn': fn}));
使用es6TemplateString
- 瀏覽器
- nodejs
npm install es6templatestring
var es6TemplateString = require('es6templatestring');
- 參見 test.js
關于更多問題,大家可以參見ejs和multiline的README文件
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!