传统的拼接字符串使用一个一个的+号,这种方式很容易在数据很多的时候出错,所以建议使用模板字符串。
优点:简洁方便
例子:
<script>
function test(name,age){
let content =`{"name":${name},"age":${age}}`;
// let content =`{"name":${name},"age":${age}}`+ `{"name":${name},"age":${age}}`;
var wrap = document.getElementById("wrap");
wrap.innerText=content
return content
}
test("陶务华",18)
</script>
实际场景:
服务端返回数据需要我们自己去进行拼接然后展示。