var obj={x:1,y:2,z:3}
obj[Symbol.iterator]=function(){
return {
next:function(){
let objArr=Reflect.ownKeys(obj) //symbol作为对象键名时,使用object.keys是无法遍历出来的
if(this.index<objArr.length-1){
let key=objArr[this.index]
this.index++
return {
value:obj[key]
}
}else{
return {
done: true
}
}
},
index:0
}
}
console.log([...obj]);
对象合并:
{...obj1,...obj2}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.container{
margin: 0 120px;
}
.main{
float: left;
width: 100%;
height: 300px;
background-color: pink;
}
.left{
position: relative;
left: -100px;
float: left;
width:100px;
height: 300px;
background-color: red;
margin-left: -100%;
}
.right{
position: relative;
right: -200px;
float: right;
width: 200px;
height: 300px;
background-color: blue;
margin-left: -100%;
}
</style>
</head>
<body>
<div class="container">
<div class="main"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
跨站请求伪造:通过恶意引导用户一次点击劫持cookie进行攻击
类型:
防御策略:
(1)同源策略(禁止外域对我们发起请求)
服务器根据http请求头中的Origin信息和referer信息判断请求是否为允许访问的站点,当Origin信息和referer信息都不存在时,直接阻止
(2)CSRF Token
服务器向用户返回一个随机token,当网站再次发起请求时,在请求参数中加入返回的token,服务器对比保存在session中token是否匹配,缺点是每次请求都需要带上token
(3)双重Cookie验证
(4)在设置Cookie的时候设置Samesite
限制cookie不能被第三方使用
let str='aaaaaabbbbbbbbbbbbcccccccccc'
function most(str){
let map=new Map()
for(let v of str){
if(map.has(v)){
map.set(v,map.get(v)+1)
}else{
map.set(v,1)
}
}
let temp=0
for(let v of map.values()){
temp=Math.max(v,temp)
}
return temp
}
console.log(most(str));
function random(min,max){
return Math.random()*(max-min)+min
}
console.log(random(20,50));
<ul>
<li></li>
<li></li>
<li></li>
</ul>
var ul=document.getElementById('ul')
ul.onclick=function(event){
event=event|| window.event
var target=event.target
if(target.nodeName==='LI'){
alert(target.innerHTML)
}
}
function check(value){
//数组和对象都为对象
if(typeof value === 'object'){
if(Object.prototype.toString.call(value)==='[object Object]'){
return 'object'
}else{
return 'array'
}
}else{
return typeof value
}
}
//将参数存放在数组中
function sum(){
let args=[...arguments]
const add=()=>{
args.push(...arguments)
return add
}
//调用toString方法获取结果
add.toString=()=>args.reduce((a,b)=>a+b,0)
return add
}
console.log(sum(1,2)(3).toString());
var login=(function(){
var div=null
return function(){
if(!div){
div=document.createElement('div')
div.innerHTML='登录成功'
div.style.display='none'
document.body.append(div)
}
return div
}
})()
document.getElementById('login').onclick=()=>{
var myLogin=login()
myLogin.style.display='block'
}
求和、计算每个元素的出现次数、去重、将多维数组转化为一维数组
1、cookie是网站为了标识用户身份而存储在用户本地终端上的数据,每次发送http请求时,会自动携带cookie,而sessionStorage、localStorage不会自动把数据发给服务器,仅在本地保存
2、cookie数据最大为4k,而其他两个为5M
3、cookie在设置的过期时间之前一直有效,sessionStorage仅在当前窗口有效,localStorage永久有效
1+'undefined' //NaN
1+'str' //'1str'
1+true //2
1+null //1+0=1
'2'>10 //false
'2'>'10' //当关系运算符两边都是数字,按照字符串对应unicode编码转换
undefined==undefined //true
undefined==null //true
null==null //true
NaN==NaN //false
//逻辑非运算符 将其他数据类型用Boolean()转换
// 0 NaN undefined null '' false 转换为boolean值为false,其他都为true
[]==0 //true Number([].valueof().toString())==0
![]==0 //true !Boolean([])
[]==![] //true Number([].valueof().toString())==0 ![]==false
[]==[] //false
{}==!{} //false {}.valueOf().toString()='[object Object]'
{}=={} //false