当前位置:网站首页>template string
template string
2022-08-03 21:11:00 【hug kitten】
模板字符串---反引号
定义
模板字符串使用反引号(`)来代替普通字符串中的用双引号和单引号.模板字符串可以包含特定语法( $ {expression} )的占位符.占位符中的表达式和周围的文本会一起传递给一个默认函数,该函数负责将所有的部分连接起来,如果一个模板字符串由表达式开头,则该字符串被称为带标签的模板字符串,该表达式通常是一个函数,它会在模板字符串处理后被调用,在输出最终结果前,你都可以通过该函数来对模板字符串进行操作处理.在模版字符串内使用反引号(`)时,需要在它前面加转义符(\).
语法
Embed the string directly
Embed a multiline string
插入表达式
带标签的模板字符串
模板标签
标签模板其实不是模板,而是函数调用的一种特殊形式.“标签”指的就是函数,紧跟在后面的模板字符串就是它的参数. 但是,如果模板字符里面有变量,就不是简单的调用了,而是会将模板字符串先处理成多个参数,再调用函数.
toString方法
函数中有toString方法,所以将1into a string for execution
eval函数
eval:will be included in the incoming stringJavaScript代码执行,If the input is not a string, it will be returned unchanged
evalVariables will be found under global variables,If the string is not defined globally, it will report undefined--> undefined
Why is the output an empty array?
We understand through a few exampleseval、反引号、模板字符串
<script>eval`${prompt(1)}`</script>
The popup function can be executed, function executed inside,The function is executed first from the template string(模板字符串中可以调用函数),再由evalWhether the execution can pass parameters.但是evalOnly the first value can be accepted,However, the title第一个参数is made in the template string执行函数${}An array of left and right strings combined,由于${}There is no value left or right,So it returns an array of two empty characters.evalOnly the previous value is accepted in the question-->空数组,The return value passed in the following function cannot be returned,So the popup window will not return no matter what value is entered.evalOutput the empty array as is.
<script>eval`${`prompt(1)`}`</script>
The popup function cannot be executed,evalThe first one received is an empty array,The second is the function that prints the template string,Because the backticks used are equivalent to will{}中的字符串prompt(1)This function prints out and passes it toeval,但是evalOnly the first value is accepted,So the second function is not accepted,也就没有执行prompt这个函数.
<script>eval`${`prompt\x281\x29`}`</script>
Same as the second example,The popup function cannot be executed,But can return empty array. Although the parentheses are coded in the question,But the effect is the same as without encoding,Backticks willpromptThe function is printed and passed toeval但是evalOnly the first parameter is accepted-->空数组,所以prompt函数没有执行.
<script>eval.call`${`prompt\x281\x29`}`</script>
因为eval是一个危险函数,So browsers are righteval有限制,换一个浏览器试试
The popup function can be executed,And can output the content entered in the pop-up window.由于callThe first value in is an empty array pointernull,所以eval被call改变指向后,指向全局(window),第二个值为prompt(1)字符串,这时evalcan be globally(window)Find this function in and execute it.The execution function returns what the function returns
cell函数的作用:可以改变this的指向
第一个参数this,如果第一个参数为null或为underfined,就将evalbound to a global variable
The second is the passed parameter
<script>eval.call`${prompt(1)}`</script>
The popup function is executed ,The template string will execute the function first,After the function is executed, an undefined string is entered,evalWhat is accepted is this string,这时evalThe string cannot be found globally,will return the string undefined.When the input string is defined, it will return the defined content.
<script>eval.call`prompt(1)`</script>
The popup function is not executed,There is no expression in it,call方法会将eval直接指向prompt,因为window对evalThis dangerous function has settings,evalCalled for an alias,官方JS都强制eval指向全局,所以evalcannot be pointed toprompt,At this point there is no second parameter,所以eval什么都没有执行.
evalThere are various forms of alias invocation,As long as it is not called directly,All are alias calls,Because the engine can only telleval()One form of this is direct invocation,will be pointed towindow.
eval.call`$alert(1)`
The reason why the value obtained is empty:alert(1)The returned value is empty,所以evalThe last received value is also empty,得到的值就是undefined.
边栏推荐
- 太香了! 阿里 Redis 速成笔记, 从头到尾全是精华!
- CC2530_ZigBee+华为云IOT:设计一套属于自己的冷链采集系统
- Transformer怎么入门?如何学习Transformer?
- Interesting opencv - record image binarization and similarity
- 安全基础8 ---XSS
- ES、Kibana 8.0安装
- error: C1083: 无法打开包括文件: “QString”: No such error: ‘QDir‘ file not found
- 剑指 Offer 16. 数值的整数次方
- 好朋友离职了,一周面试了20多场,我直呼内行
- tidyverse based on data.table?
猜你喜欢
随机推荐
Often forget HiFlow 】 【 check-in?Use tencent cloud scenario connector to remind you every day.
Likou 59 - Spiral Matrix II - Boundary Judgment
leetcode 1837. The sum of the digits in the K-base representation
软考系统分析师备考经验分享:论持久战
ES6 deconstruction assignment - array object deconstruction and deconstruction
华为设备配置VRRP与BFD联动实现快速切换
How can a cloud server safely use local AD/LDAP?
StoneDB 助力 2022 开放原子全球开源峰会
canvas螺旋动画js特效
小朋友学C语言(3):整数、浮点数、字符
独立站卖家在哪些平台做社交媒体营销效果最好?
leetcode 268. 丢失的数字(异或!!)
收藏-即时通讯(IM)开源项目OpenIM-功能手册
卷起来!阿里高工携 18 位高级架构师耗时 57 天整合的 1658 页面试总结
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
XSS线上靶场---haozi
leetcode 072. Finding Square Roots
False label aggregation
用 setTimeout 来实现 setInterval
图神经网络怎么入门?一文带你了解图神经网络入门路径-GNN入门