内容导读
(1)函数体内的this对象,就是定义时所在的对象,而不是用时所在的对象。(2)不能当作构造函数,也就是说,不能用new命令,否则会抛出一个错误。(3)不能用arguments对象,该对象在函数体内不存在。(4)不能用yield命令,因而箭头函数不可以使用作 Generator 函数。上面四点中,第一点尤其值得注意。this对象的指向是可变的,但是在箭头函数中,它是固定的。
1、写一个可以遍历对象和数组的通使用forEach函数?
pre class="brush:js;toolbar:false"> var key; if(obj instanceof Array){ obj.forEach(function(item,index){ fn(index,item) }) }else{ for(key in obj){ fn(key,obj[key]) } }}
2、JavaScript 中如何获取当前的时间戳
pre class="brush:js;toolbar:false">Date.now()new Date().getTime()+new Date()
3、手写ajax
pre class="brush:js;toolbar:false">var xhr=new XMLHttpRequest()xhr.open('GET','/api',false);xhr.onreadtstatechange = function(){ if(xhr.readyState==4 && xhr.status==200){ conse.log(xhr.responseText) }}xhr.send(null);
4、跨域
pre class="brush:js;toolbar:false">起因:同源策略,cookie、localstorage、indexdb无法读取、dom无法操作、ajax无法发送前后台通信方式:ajax、websocket、CORS处理:JSONP:网页动态插入