当前位置: 首页 > Web前端 > HTML5

面试题分享

时间:2023-04-05 20:22:50 HTML5

作为做了一年多的前端,总觉得自己应该写自己的博客。其实我早就想写了,就是不知道写什么。感觉自己还是个新手,不想自己写的误导孩子。前段时间,在我们班级群里看到一张分享的照片。上面有一些话题。我自己做的。我觉得还是可以总结分享一下的。听说是阿里的话题。有点技术含量。好吧,不多说就说吧。本博客只是对这些问题的个人回答。个别问题我也在百度上搜索过。总觉得自己的写作方法还可以继续优化。这不是很好。希望对前端同学有所帮助。大神们绕路吧~题目如下:1.去除字符串数组中的重复项,例如:['1','2','1','3']-->['1','2','3'];代码:functionquchong(arr){vara=[];for(vari=0,len=arr.length;i["aB":1]代码1:functiontrans(obj){vararr=[];varnewObj={};变量pos,键;for(varpinobj){key=p;pos=p.indexOf('_');p=p.slice(0,pos)+''+p.substr(pos+1,1).toLocaleUpperCase()+''+p.slice(pos+2);newObj[p]=obj[键];}returnnewObj;}代码2:functiontrans(obj){varkeys=Object.keys(obj);varnewObj={};变量键;for(vari=0,len=keys.length;i

用原生js使这个div可拖动;代码:window.onload=function(){varbox=document.getElementById("draggable");box.onmousedown=function(){document.onmousemove=function(event){varev=event||}窗口事件;box.style.left=(ev.clientX-box.offsetHeight/2)+'px';box.style.top=(ev.clientY-box.offsetWidth/2)+'px';}}box.onmouseup=function(){document.onmousemove=null;}}5.写一个函数,实现数字格式化输出。例如,如果您输入999999999,则输出将为999,999,999。代码:函数formarNum(num,sep){sep=sep||',';varreg=/\B(?=(\d{3})+(?!\d))/g;varstr=num.toString().replace(reg,sep);returnstr;}注意:如果数字不是很大,可以直接使用toLocaleString()方法;如果要格式化的数字有小数部分,需要截取小数部分,格式化整数部分,然后拼接输出结果。toLocaleString方法使用截图:6.写一个函数parseQueryString,用于将URL参数解析成一个对象。代码:函数parseQuryString(url){varobj={};变量str='';变量arr=[];变量项='';if(url.indexOf('?')!=-1){str=url.split('?')[1];arr=str.split('&');for(vari=0,len=arr.length;i0){for(vari=0,len=arr.length;i0){flatten(arr[i])}else{result.push(arr[i]);}}}returnresult;}注意:这里使用了递归的方法,如果数组中的元素仍然是一个数组,并且子数组的长度大于零,则继续调用这个方法。8.写一个类EventEmitter实现简单的发布订阅功能:conste=newEventEmitter();e.on('update',function(data){console.log(data)});e.emit('update','信息');代码:函数EventEmitter(event,cb){};EventEmitter.prototype.on=function(eventName,cb){this[eventName]=cb;}EventEmitter.prototype.emit=function(eventName,message){this[eventName](message);}vare=newEventEmitter();e.on('update',function(data){console.log(data)});e.emit('更新','消息');结果截图:注意:如果要链(xx.xx.xx)使用发布订阅功能,需要在on中returnthis并emit。例如:函数EventEmitter(event,cb){};EventEmitter.prototype.on=function(eventName,cb){this[eventName]=cb;归还这个;}EventEmitter.prototype.emit=function(eventName,message){这个[eventName](message);归还这个;}vare=newEventEmitter();e.on('update',function(data){console.log(data)}).on('go',function(data){alert(data)});e.emit('更新','消息').emit('去','回家');结果截图:注:我的第一篇博文写完了,分享一下方法可能不是最好的,但我觉得还是比较容易理解的。我觉得学习应该是一种主动的行为,所以共享方法中没有注解信息。如果对你有帮助,请多多鼓励。在我看来,前端知识是一片汪洋大海,很多技术还在往里面导入。希望这次分享只是一个开始,以后能和大家分享更好的前端知识。