美文网首页
21.模板字符串和标签模板字符串

21.模板字符串和标签模板字符串

作者: 静昕妈妈芦培培 | 来源:发表于2022-02-10 11:28 被阅读0次

ES6新增了模板字符串,用于字符串拼接

const name = "why";
const age = 18;
const height = 1.88;

const info = `my name is ${name},age is ${age},height is ${height}.`; //变量
const info1 = `age double is ${age * 2}`; //表达式

function getDoubleAge() {
  return age * 2;
}
const info2 = `double age is ${getDoubleAge()}`; //函数调用
console.log(info)
console.log(info1)
console.log(info2)
image.png

ES6新增了标签字符串

const name = "why";
const age = 18;
const height = 1.88;
function foo(arr, arg1, arg2, arg3) {
  console.log(arr, arg1, arg2, arg3);
}
foo``; 
//[ '' ] undefined undefined undefined
foo`my name is ${name},age is ${age},height is ${height}`; 
//[ 'my name is ', ',age is ', ',height is ', '' ] why 18 1.88
foo`my name is ${name},double age is ${age * 2},height is ${height}`; 
//[ 'my name is ', ',double age is ', ',height is ', '' ] why 36 1.88

标签模板字符串执行结果:

  • 函数的第一个参数为数组,是模板字符串中的字符串被${}切割成的数组
  • 除了第一个后的参数依次为模板字符串中${}的值

非常感谢王红元老师的深入JavaScript高级语法让我学习到很多 JavaScript 的知识

相关文章

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

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

  • ES6-字符串模板

    模板字符串使用案例一 标签模板字符串 标签模板字符串案例 标签函数的用途,可以用来过滤用户输入

  • ECMAScript6 学习(一)

    字符串的扩展 字符串的遍历器接口for...of循环 模板字符串 字符串中嵌入变量 标签模板 模板字符串可以紧跟在...

  • ES6字符串扩展

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

  • 21.模板字符串和标签模板字符串

    ES6新增了模板字符串,用于字符串拼接 ES6新增了标签字符串 标签模板字符串执行结果: 函数的第一个参数为数组,...

  • ES6模版字符串

    初探ES6:字符串模板 && 标签模板 关键词:``,${} 字符串模板: 在ES6之前我们要在html或者con...

  • es6:模板字符串

    模板字符串:${变量名}标签模板:是函数调用的一种特殊形式,标签指的是函数紧跟在后面的模板字符串就是它的参数 st...

  • Django标签、转义及验证码生成

    一、模板 组成 作用 优点 模板处理 二、模板使用 变量 变量不存在,则插入空的字符串 标签 标签for - in...

  • 标签模板及其应用

    1、标签模板 标签模板其实不是模板,而是函数调用的一种特殊形式。“标签”指的就是数,紧跟在后面的模板字符串就是它的...

  • ES6(4)、新版字符串

    1、模板字符串 模板字符串使用反引号 (``) 来代替普通字符串中的用双引号和单引号。模板字符串可以包含特定语法(...

网友评论

      本文标题:21.模板字符串和标签模板字符串

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