码迷,mamicode.com
首页 > 其他好文 > 详细

【ES6】模拟字符串拼接

时间:2017-07-14 23:04:25      阅读:1202      评论:0      收藏:0      [点我收藏+]

标签:write   ges   script   方便   对象   type   blog   效果   utf-8   

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>ES6 - 模拟字符串拼接</title>
</head>
<body>
<h1></h1>
</body>
<script>
    var obj = {
        name: ‘Alice‘,
        txt: ‘你好‘,
        day: ‘2017-05-30‘,
        thing: ‘Something happened!‘
    };

    document.write(‘<p><b>‘ + obj.name + ‘</b>, ‘ + obj.txt + ‘, 还记得‘ +
            ‘那天吗?‘ + obj.day + ‘, ‘ + obj.thing + ‘</p>‘);   // 原始写法

    document.write(`<p><b>${obj.name}</b>, 
${obj.txt}, 还记得
那天吗?${obj.day},
${obj.thing}</p>`); // ES6写法 </script> </html>

  效果图如下:

  技术分享

  可以发现,原始字符串拼接的写法相当繁琐,且遇到换行还要格外小心,现在有了ES6写法,感觉方便多了;

  只需要在所要拼接的字符串首尾加`反引号`(Tab上第一个键),所要拼接的对象放在${obj}内部,且支持换行,麻麻再也不用担心你的字符串拼接了。

【ES6】模拟字符串拼接

标签:write   ges   script   方便   对象   type   blog   效果   utf-8   

原文地址:http://www.cnblogs.com/likwin/p/7172416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!