美文网首页
8. 字符串拼接和模板字符串

8. 字符串拼接和模板字符串

作者: yaoyao妖妖 | 来源:发表于2021-01-12 14:53 被阅读0次

字符串拼接
const a = 20
const b = 10
const c = 'javascript'
ES5

const str = 'my age is' + (a + b) + 'i love' + c

ES6

const str = `my age is ${a + b} i love ${c}`
console.log(str)

ES5字符串变量

const retailPrice = 20
const wholeSalePrice = 16
const type = 'retail'
let showTxt = ''
if (type === 'retail') {
  showTxt = '您此次的购买单价是:' + retailPrice
} else {
  showTxt = '您此次的购买的批发价是:' + wholeSalePrice
}
console.log(showTxt)

ES6模板字符串

function Price (strings, type) {
  let s1 = strings[0]
  const retailPrice = 20
  const wholeSalePrice = 16
  let showTxt
  if (type === 'retail') {
    showTxt = '购买单价是:' + retailPrice
  } else {
    showTxt = '购买的批发价是:' + wholeSalePrice
  }
  return `${s1}${showTxt}`
}

let showTxt = Price`您此次的${'retail'}`
console.log(showTxt)

ES6换行

let s1 = `我是第一行

我是第二行`

console.log(s1)

学习视频记录

相关文章

  • 实现模板引擎

    字符串替换 Template(模板) 目录 字符串拼接 string format(字符串格式化) 模板替换 自制...

  • 模板字符串19-10-16

    1.模板字符串:简化字符串的拼接

  • es6学习笔记整理(三)字符串扩展

    字符串新增特征: 1、Unicode表示法 2、遍历接口 3、模板字符串 数据和模板拼接成带结果的字符串 ``加变...

  • ES6字符串扩展

    字符串的Unicode表示法 字符串的遍历器接口 模板字符串` 长字符串换行 变量拼接 嵌套模板 带标签的模板字符...

  • ES6常用新特性

    ES6新特性介绍 模板字符串 模板字符串实现字符串拼接 模板字符串实现多行字符串 结构赋值 对象的解构赋值 数组的...

  • 模板字符串

    模板字符串 : 简化字符串的拼接 模板字符串必须用 `` 包含 变化的部分使用${xxx}定义

  • ES6-字符串方法及其实现

    1.模板字符串 模板字符串替换+操作符,来拼接字符串,并且支持换行: 标签模板: 标签模板其实不是模板,而是函数调...

  • 8. 字符串拼接和模板字符串

    字符串拼接const a = 20const b = 10const c = 'javascript'ES5 ES...

  • ES6之字符串的扩展(上)

    模板字符串 在传统的拼接字符串中,使用的是‘+’进行拼接: 可以看出用+进行拼接字符串比较繁琐,尤其是当字符串特别...

  • es6小结

    模板字符串字符串拼接不需要+,而是用``,变量用${变量名}表示 Tips:模板字符串里的变量的值取模板字符串定义...

网友评论

      本文标题:8. 字符串拼接和模板字符串

      本文链接:https://www.haomeiwen.com/subject/luhqjktx.html