前端工程师面试题JavaScript部分(第五季)
alert('1'+1)//11,不是2,这个容易忘
第二题,都是基础的,很容易掉坑里
(function(){ var a = b = 6; })() console.log(b);//6全局变量声明
第三题声明提前
function test(){ console.log(a); console.log(foo()); var a = 1; function foo(){ return 2; } } test();
这个很简单
var fullname = 'John Doe'; var obj = { fullname : 'Colin Ihrig', prop : { fullname: 'Aurelio De Rosa', getFullname : function(){ return this.fullname; } } } console.log(obj.prop.getFullname()); var test = obj.prop.getFullname; console.log(test())'
接下来手写事件代理
function delegateEvent (interfaceEle, selector, type, fn){ if(interfaceEle.addEventListener){ interfaceEle.addEventListener(type,eventfn,false); } else{ interfaceEle.attach('on'+type,evetnfn); } function eventfn(e){ var e = e|| window.event; var target = e.target || e.srcElement; if(matchSelecter(target,selector)){//a 是否是a if(fn){ fn.call(target,e); } } } } function mathcSelector(ele,selector){ if(selector.charAt(0)==='#'){ return ele.id == selector.slice(1); } if(selector.charAt(0)=='.'){ return (" " + ele.className+' '.indexOf(' ' + selector.slice(1)+' ') != -1) return ele.targetName.toLowerCase()===slector.toLowerCase(); } } var odiv = docyment.getElementById("oDiv"); delegateEvent(oDiv,'a','click',function(){ alert(1) })