- 1使用模板字符串而不是拼接客串
(反单引号)
//对接看了又看的数据
let seeStr = '';
let seeList = obj.see_again;
for(let i of seeList){
seeStr += `<div class="tl-content2" onclick="productDetailJs.goSee(${i.xin_chong_id});">
<div class="tl-flex" style="width:111px;height:85px;align-items: center;margin:0 auto;background: url(${i.head_pic}) no-repeat center top;background-size: 100%">
</div>
<div class="tl-flex2" style="width:120px;height:85px;margin-left: 12px">
<div class="tl-font-16-b">${i.zh_cate_name} ${i.gender === 1 ? '公' : '母'}</div>
<div class="tl-font-14-5 tl-gray">${i.birth_str} | ${i.yiaomiao_cnt}针</div>
<div class="tl-font-12-5">批发价:<strong class="tl-price tl-red">¥${i.price}</strong> </div>
</div>
<div class="tl-flex2" style="flex:1;height:85px;">
<!--<img src="../assets/img/protect.png" alt="" class="tl-img-15" style="margin:auto auto;">-->
<div class="tl-font-14-5 tl-gray img-r" >质保${i.quality_assurance_time}天</div>
</div>
</div>
<div class="tl-line1"></div>`;
}
$('#seeData').html(seeStr);
- 2 for…of是for循环的首选类型
for…of语句在可迭代对象(Array,Map,Set,String,TypedArray,arguments 对象等等)上创建一个迭代循环,为每个不同属性的值执行语句。
let arr = [123,'abc']
for(let item of arr){
console.log(item)
}
//123
//abc
for…in用于遍历数组时,可以将数组看作对象,数组下标看作属性名。但用for…in遍历数组时不一定会按照数组的索引顺序。
使用for…in循环时,获得的是数组的下标;使用for…of循环时,获得的是数组的元素值。
for…of遍历Map时,可以获得整个键值对对象:
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);
for (let entry of iterable) {
console.log(entry);
}
// ["a", 1]
// ["b", 2]
// ["c", 3]
原文地址:https://blog.csdn.net/qq_32657025/article/details/79537340
- 3 使用单引号,而不是双引号
普通的字符串用单引号(')分隔,而不是双引号(")。
提示:如果字符串包含单引号字符,可以考虑使用模板字符串来避免转义引号。
// bad
let directive = "No identification of self or mission."
// bad
let saying = 'Say it ain\u0027t so.';
// good
let directive = 'No identification of self or mission.';
// good
let saying = `Say it ain't so`;