1const obj = { 2 foo: "foo", 3 bar: "bar" 4} 5observe(obj) 6delete obj.foo // no ok 7obj.jar = 'xxx'// no ok
当我们对一个数组进行监听的时候,并不那么好使了
1 2 3 4 5 6 7
1const arrData = [1,2,3,4,5]; 2arrData.forEach((val,index)=>{ 3 defineProperty(arrData,index,val) 4}) 5arrData.push() // no ok 6arrData.pop() // no ok 7arrDate[0] = 99// ok
var obj={a:1}, handlers={ get(target,key,context){ console.log('accessing:',key); return target[key]; } }; var p=Proxy.revocable(obj,handlers);//返回一个有两个属性proxy和revoke的对象 const pobj=p.proxy; const prevoke=p.revoke; console.log(pobj.a); prevoke();//代理被取消后,任何对它的访问都会抛出错误 pobj.a()//Uncaught TypeError: Cannot perform 'get' on a proxy that has been revoked
var handlers={ get(){ throw"no such property/method" }, set(){ throw"no such property/method"
} } var pobj=newProxy({},handlers); var obj={ a:1, foo(){ console.log("a:",this.a) } } Object.setPrototypeOf(obj,pobj); obj.a=3; obj.foo(); obj.b=4;//Uncaught Error: no such property/methodxxc
var form = document.getElementById("myForm"); let xhr=new XMLHttpRequest(); var formData = new FormData(form); xhr.onreadystatechange=function(){ if(xhr.readyState==4){ if((xhr.status>=200&&xhr.status<300)||xhr.status===304){ alert(xhr.responseText) }else{ alert("Request was unsuccessful:"+xhr.status) }
} xhr.open("post","http://127.0.0.1:3007/api/login",true) xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')//设置发送请求的头部 xhr.timeout=1000;//设置超时 xhr.ontimeout=function(){ alert("Request didn not return in a second") } xhr.send(serialize(form)) //使用FormData //xhr.send(new FormData(form)) }
let r1=new Request('http://foo.com'); let r2=new Request(r1,{method:'POST'}); console.log(r1.method);//GET console.log(r2.method);//POST //这种克隆方式不能总得到一模一样的副本
使用clone
请求体被读取后再克隆会导致抛出TypeError
1 2 3 4 5 6 7
let r=new Request('http://foo.com'); r.clone(); new Request(r);//没有错 r.text();//设置bodyUsed为true r.clone();//TypeError
var a=42; var b=[42]; a==b;//true,变成42=='42'=>42==42 var a="abc"; var b=Object(a); a===b;//false a==b;//true,b通过ToPrimitive强制类型转换为"abc" //没有对应封装对象,null和undefined不能被封装,下面返回false var a==null; var b=Object(a); a==b;//false var a=undefined; var b=Object(a); a==b;//false var a=NAN; var b=Object(a); a==b;//false,因为NAN==NAN本身是false
var mysym=Symbol("my on symbol") console.log(mysym.toString())//Symbol(my on symbol) console.log(typeof mysym);//symbol var a={} a[mysym]="foorbar" console.log(Object.getOwnPropertySymbols(a));//通常用来命名私有或特殊属性,替代下划线(_)前缀的属性 //[Symbol(my on symbol)]