当前位置:网站首页>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.
边栏推荐
- leetcode 1837. K 进制表示下的各位数字总和
- LitJson报错记录
- Li Mu hands-on learning deep learning V2-BERT fine-tuning and code implementation
- Use setTimeout to realize setInterval
- idea2021配置svn报错Cannot run program “svn“ (in directory “xxx“):CreateProcess error=2,系统找不到指定的文件
- Linux操作Jmeter(附带:关于连接上redis无法进行写入操作的问题),JMeter配置多用户进行压力测试
- nvm的使用 nodejs版本管理,解决用户名是汉字的问题
- ES6--residual parameters
- Power button 206 - reverse list - the list
- Zero trust, which has been popular for more than ten years, why can't it be implemented?
猜你喜欢
随机推荐
LeetCode_Digit Statistics_Medium_400. Nth Digit
Engineering Effectiveness Governance for Agile Delivery
15 years experience in software architect summary: in the field of ML, tread beginners, five hole
Cesium 修改鼠标样式
[kali-vulnerability scanning] (2.1) Nessus download and installation (on)
Leetcode 125. Verify palindrome string
6. XML
【kali-漏洞利用】(3.2)Metasploit基础(上):基础知识
idea2021.1.3版本如何启动多个客户端程序
leetcode 268. Missing Numbers (XOR!!)
5 款漏洞扫描工具:实用、强力、全面(含开源)
TweenMax.js向日葵表情变化
B. Paranoid String
XSS测试
XSS练习---一次循环和两次循环问题
小朋友学C语言(1):Hello World
3种圆形按钮悬浮和点击事件
从开发到软件测试:除了扎实的测试基础,还有哪些必须掌握 ?
深度学习怎么入门?零基础快速入门深度学习
Likou 59 - Spiral Matrix II - Boundary Judgment