首页 > 其他 > 详细

ES6 之 04.模板(标签)字符串

时间:2020-06-13 11:33:06      阅读:50      评论:0      收藏:0      [点我收藏+]

索引

模板字符串
const person=‘Jelly‘;
const age=20;

const  intro=`${person} is ${age} years old`;

const html=`
 <div class="greet">
    <p>hello</p>
 </div>
`.trim()
const Jelly={
    name:‘Jelly‘,
    date:‘2020-02-10‘,
    todos:[
        {name:‘sleep in bed‘,completed:false},
        {name:‘music‘,completed:true},
        {name:‘water‘,completed:false}
    ]
}

function renderTodos(todos) {
    const str=
        `<ul>
            ${todos.map(todo =>
            `
             <li>
              ${todo.name} ${todo.completed ? ‘1‘ : ‘0‘}
             </li>
            `).join(‘‘)
            }
          </ul>
        `
    return (str)

}
const tpl=`
<div class="panel">
  <div class="panel-header">${Jelly.name}</div>
  <div class="panel-body">
    ${renderTodos(Jelly.todos)}
   </div>
   <div class="panel-footer">${Jelly.date}</div>
</div>
`
模板标签字符串
const user=‘marry‘
const topic="learn to use Markdown"

const sentence=highlight`${user} has commented on your topic ${topic}`

function highlight(strings,...values){
    debugger
}

strings与values

  • 变量在模板开头或者结尾或多出空格
Local
strings: (3) ["", " has commented on your topic ", "", raw: Array(3)]
values: (2) ["marry", "learn to use Markdown"]
this: Window
const user=‘marry‘
const topic="learn to use Markdown"

const sentence=highlight`${user} has commented on your topic ${topic}`

function highlight(strings,...values){
    const  highlighted=values.map(value =>
        `<span class="highlight">${value}</span>`
    )
    let str=‘‘;

    strings.forEach((string,i)=>
        str+=`${string}${highlighted[i]||‘‘}`
    )
    return str

}

进化html里输入的脚本:sanitize

ES6 之 04.模板(标签)字符串

原文:https://www.cnblogs.com/HKUI/p/13112106.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!