ES6字符串模板

语法

用 `` 代替了单引号和双引号

let message = `hello world`;

一、多行字符串

以下效果均需在浏览器打印输出查看,在页面上显示不会出现。
页面上会统一显示为一个空格

ES5字符串中如果想要换行,需要在字符串中加\n

var message = "hello\nworld"

现在只需直接回车换行就可以

var message = `hello
world`

而且你在字符串之间输入的空格也会原样显示

二、字符串占位符

语法:${}
占位符都是JavaScript表达式,包括变量、运算式、函数调用等。

let message = "world";
let info = `hello ${message}`;
console.log(info); // hello world
let info = `hello ${7 * 3}`;
console.log(info); // hello 21
function say () {
    return 'function';
}    
let message = `hello ${ say() }`;
console.log(message); // hello function

网站导航

网站导航

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容