ECMAScript6 模版字符串
三、模版字符串
JavaScript’s strings have been fairly limited when compared to those in other languages. Template strings add new syntax to allow the creation of domain-specific languages (DSLs) for working with content in a way that is safer than the solutions we have today. The description on the template string strawman was as follows
与其他的语言相比,js的字符串受到了很大的限制。模版字符串是新增加的语法允许创建开发领域特定语言(DSLs)来处理内容,使用这种方式比我们现在使用的解决办法更加安全。关于模版字符串的解释如下:
This scheme extends ECMAScript syntax with syntactic sugar to allow libraries to provide DSLs that easily produce, query, and manipulate content from other languages that are immune or resistant to injection attacks such as XSS, SQL Injection, etc.
这个计划通过“语法糖”的形式扩展了ECMAScript的语法,允许各种库提供DSLs语法,使得在其他语言内创建、查询和操作内容更加容易,可以免除和抵抗注入攻击例如XSS和SQL注入等。
In reality, though, template strings are ECMAScript 6’s answer to several ongoing problems in JavaScript:
实际上,模版字符串是ES6用来解决js中现存的几个问题的方法:
- Multiline strings - JavaScript has never had a formal concept of multiline strings.
- 多行字符串-js一直没有多行字符串的正式的概念。
- Basic string formatting - The ability to substitute parts of the string for values contained in variables.
- 基本的字符串格式化-能够替换变量中所包含的部分字符串的值
- HTML escaping - The ability to transform a string such that it is safe to insert into HTML.
- HTML转义-能够转化字符串这样插入到html中更加安全。
Rather than trying to add more functionality to JavaScript’s already-existing strings, template strings represent an entirely new approach to solving these problems.
不是在js已经存在的字符串上增加大量的功能,模版字符串展现出一种全新的解决这些问题的方法。
基本语法
At their simplest, template strings act like regular strings that are delimited by backticks (`
) instead of double or single quotes. For example:
最简单的,模版字符串和普通字符串一样,只不过界定符号是“`”而不是双引号或者单引号。例如:
let message = `Hello world!`;console.log(message); // "Hello world!"
console.log(typeof message); // "string"
console.log(message.length); // 12
This code demonstrates that the variable message
contains a normal JavaScript string. The template string syntax only is used to create the string value, which is then assigned to message
.
这段代码展示了变量message包含一个普通的js字符串。模版字符串的语法只是用来创建字符串值,接着赋值给message。
If you want to use a backtick in your string, then you need only escape it by using a backslash (\
):
如果你想在字符串中使用`,你需要通过凡斜杠(\)转义一下:
let message = `\`Hello\` world!`;console.log(message); // "`Hello` world!"
console.log(typeof message); // "string"
console.log(message.length); // 14
There’s no need to escape either double or single quotes inside of template strings.
在模版字符串内部,不需要转义单、双引号。
多行字符串
Ever since the first version of JavaScript, developers have longed for a way to create multiline strings in JavaScript. When using double or single quotes, strings must be completely contained on a single line. JavaScript has long had a syntax bug that would allow multiline strings by using a backslash (\
) before a newline, such as:
自从第一代js版本开始,开发者就渴望能够有一种方法在js中创建多行字符串。当使用单、双引号的时候,字符串必须包含在一行内。js长期以来都有一个语法错误-允许多行字符串在新的一行开始使用反斜杠(\),例如:
let message = "Multiline \
string";
Despite this working in all major JavaScripte engines, the behavior was defined as a bug and many recommended avoiding its usage.
尽管这种方式可以在所有的主要js引擎下工作,但是这种行为定义了一个bug,许多建议避免使用这种方式。
Other attempts to create multiline strings usually relied on arrays or string concatenation, such as:
其他的试图创建多行字符串通常依赖于数组和字符串连接,例如:
let message = ["Multiline ","string"
].join("");let message = "Multiline " +"string";
All of the ways developers worked around JavaScript’s lack of multiline strings left something to be desired.
开发者所使用的这些方式都是因为js缺少多行字符串。
Template strings make multiline strings easy because there is no special syntax. Just include a newline where you want and it shows up in the result. For example:
模版字符串使得多行字符串变得简单因为没有什么特殊的语法。只是增加一个新行当你需要和希望在结果中显示的时候。例如:
let message = `Multiline
string`;console.log(message); // "Multiline// string"
console.log(message.length); // 16
All whitespace inside of the backticks is considered to be part of the string, so be careful with indentation. For example:
在``之内的所有空格都被认为是字符串的一部分,所以排版的时候要小心。例如:
let message = `Multilinestring`;console.log(message); // "Multiline// string"
console.log(message.length); // 31
In this code, all of the whitespace before the second line of the template string is considered to be a part of the string itself. If making the text line up with proper indentation is important to you, then you consider leaving nothing on the first line of a multiline template string and then indenting after that, such as this:
在这段代码中,模版字符串第二行之前的所有空格都被认为是字符串本身的一部分。如果正确的文本排列很重要,那么模版字符串第一行之前不要有任何内容然后在排版,例如:
let html = `
<div><h1>Title</h1>
</div>`.trim();
This code begins the template string on the first line but doesn’t have any text until the second. The HTML tags are indented to look correct and then the trim()
method is called to remove the initial (empty) line.
这段代码中,模版字符串从第一行开始但是直到第二行才有文本内容。HTML标签的排版看着是正确的接着使用trim方法删除第一行。
转载于:https://www.cnblogs.com/avivaWang/p/4341095.html
ECMAScript6 模版字符串相关推荐
- 20181210-es6(letconst解构模版字符串原理 展开运算符、剩余运算符运用 深拷贝原理 reduce原理 箭头函数)...
变量声明 var 特点: 1.可以重复声明 2.不能定义常量 3.不支持块级作用域 复制代码 let //1.不存在预解释 变量提升 //2.暂时性死区 //3.具备块级,同一块内不能重复声明;let ...
- 03-1.JavaScript基础语法略写/模版字符串
基础语法 参考前端基础之JavaScript - Q1mi - 博客园 略写原因 由于后续主要用jQuery编写,jQuery简化编程.大概了解JavaScript语法即可. jQuery是一个轻量级 ...
- JS基础学习(十):字符串的大小写转换、trim()方法、JS字符串截取和模版字符串
1.字符串的大小写转换 字符串将小写字母转化为大写字母使用:toUpperCase 字符串将大写字母转化为小写字母使用:toLowerCase ASEDECFSXV 转化小写 ...
- ie模版字符串_【探秘ES6】系列专栏:模版字符串
[探秘ES6]系列专栏(四):模版字符串 为什么80%的码农都做不了架构师?>>> ... [探秘ES6]系列专栏 [探秘ES6]系列专栏(一):ES6简介 [探秘ES6]系列专栏( ...
- vue语法 `${ }` (模版字符串)
const name = '小缘' const age = 14 console.info(`大家好,我叫${name},今年${age}岁了`) // 等价于 console.info('大家好,我 ...
- ie模版字符串_字符串模板、模板字符串、Vue中使用template等等。
1:模板字符遇新是直朋能到串 传统的一如分算需上来处一定迹面数一跳这件我子作JS语言,输出模板新直能分支调二浏页器朋代说,事刚需求是这样写的: $('#result').append( 'There ...
- ie模版字符串_字符串(String)模板引擎被视为是有害的
用模板和一些数据来生成HTML不是一项特别繁重的任务.那些你必须要为HTML做的事才是效率低下的部分. 假如你有一个代表改变数据的UI: Current score: {{currentScore}} ...
- vue学习之二ECMAScript6标准
一.ECMAScript6标准简述 ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScr ...
- 「译」在JavaScript中将值转换为字符串的5种方法
原文: 5 Ways to Convert a Value to String in JavaScript 如果您关注Airbnb的样式指南,首选方法是使用"String()"? ...
最新文章
- R6010-abort() has been called,Error openning file ../../modules/highgui/src/cap_ffmpeg_impl.hpp解决方法
- 腾讯/字节/华为/旷视 2022届实习面经—计算机视觉方向
- 数据蒋堂 | 从一道招聘考题谈起
- 美国AI博士指出:60天掌握Python全栈需要...
- 使用maven编译YCSB0.1.4对cassandra进行性能测试
- windows和linux加密u盘,linux挂载windows的各种格式U盘
- 计算机文件自动备份到移动硬盘,如何让电脑文件自动备份到指定的邮箱里?或者备份到指定的磁盘里...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 列表表达式/列表重写...
- 【数字信号】基于matlab GUI DTMF双音多频电话拨号仿真【含Matlab源码 805期】
- IE和ff下javascript解释的区别
- 第四章 Python数据预处理之划分训练集与测试集
- 摄影师伴侣——ON1系列摄影后期图像处理软件合集
- 备用计算机机房管理制度,计算机机房管理制度
- 自己搭的12V 电机驱动电路设计
- 苹果app退款_苹果App Store应用,5月最新退款
- 微信小程序 手机号 邮箱正则表达式验证
- 手机\固定电话座机呼叫转移设置方法
- 系统迁移后无法正常开机相关心得
- 单片机 STM32 HAL PCF8574 例子代码
- NBA球星管理系统 v1.0
热门文章
- 面试中几个基本的重要问题总结
- 哈尔滨有线电视频道表
- can总线报文是固定的吗_CAN总线传输协议
- php session传数组,php把数组存到session的方法
- html单击出现下拉菜单,*OnClick实现点击主菜单时出现下拉菜单,已实现但是有问题,求助!*...
- 随输入动态改变ui_深入详解 Jetpack Compose | 优化 UI 构建
- 设计模式:000设计模式与面向对象介绍
- aspose 转pdf表格大小乱了_自己写了一个小工具类:pdf转word,没有页数和大小限制,保真!...
- java 本年第一天_JAVA获取本周 本月 本年 第一天和最后一天
- Java 算法 学做菜