JAVASCRIPT中经典面试题
1 //1、try catch finally中的return 2 var n=1; 3 function fun(){ 4 try{ 5 n++; 6 m++;//报错 7 return n; 8 }catch(err){ 9 n++; 10 return n; 11 }finally{ 12 n++; 13 //return n; 14 } 15 } 16 console.log(fun());//3 17 console.log(n);//4
/*作用域和作用域链*/ var n=10; function fun(n){ n--; console.log(n); } console.log(fun(n)); //9 console.log(n); //10
1 /*new Function*/ 2 var i=5; 3 function fun(){ 4 var i=4; 5 //var f1=new Function("console.log(i)"); 6 var f1=function(){console.log(i);}; 7 console.log(f1); 8 f1(); 9 } 10 fun();//4
1 /*callback 与 this*/ 2 var o={ 3 n:1, 4 fun:function(){ 5 var self=this;//留住this 6 console.log(self.n++); 7 setTimeout(function(){ 8 self.fun(); 9 },1000); 10 } 11 } 12 o.fun();//this-->o
1 /*DOM中的闭包问题*/ 2 function fun(){ 3 var li=document.getXX...; 4 btn.onclick=function(){ 5 li.xx=xx; 6 }//li将永远无法释放,即使刷新页面也不行 7 }
1 /*引用类型的共有属性*/ 2 function Student(){} 3 Student.prototype.arr=[]; 4 var lilei=new Student(); 5 var hmm=new Student(); 6 lilei.arr.push(1);//this[this.length]=1 7 hmm.arr[1]=2; 8 console.log(lilei.arr);//[1,2] 9 console.log(hmm.arr);//[1,2]
1 /*继承父类型共有方法*/ 2 function Student(){} 3 Student.prototype=[]; 4 var lilei=new Student(); 5 lilei.push(1);//this[this.length]=value; 6 var hmm=new Student(); 7 hmm.push(2); 8 console.log(lilei); 9 console.log(hmm);
1 /*私有属性,公有属性*/ 2 function Student(){ 3 var prop="私有属性";//私有属性 4 this.prop="公有属性"; //公有属性 5 var fun=function(){//私有方法 6 console.log("调用私有方法"); 7 console.log(prop); 8 } 9 this.fun=function(){//公有方法 10 console.log("调用公有方法"); 11 fun();//this-->window 12 console.log(prop); 13 console.log(this.prop); 14 } 15 } 16 var lilei=new Student(); 17 lilei.fun();//公有
1 //阻碍事件 2 for(var i=0;i<3;i++){ 3 setTimeout(function(){ 4 console.log(i); 5 },10) 6 } 7 alert("hello");
1 function MyObj(){ 2 this.p.pid++; 3 } 4 MyObj.prototype.p={"pid":0}; 5 MyObj.prototype.getNum=function(num){ 6 return this.p.pid+num; 7 } 8 var obj1=new MyObj(); 9 var obj2=new MyObj(); //p.pid=2 10 console.log(obj1.getNum(1)+obj2.getNum(2));//7
1 /**/ 2 function fun(n,o){ 3 console.log(o); 4 return {fun:function(m){ 5 return fun(m,n); 6 } 7 } 8 } 9 var a=fun(0);//undefined 10 //a-->{fun:function(m){return fun(m,n)}} 11 a.fun(1); a.fun(2); a.fun(3); //0 0 0 12 var b=fun(0).fun(1).fun(2).fun(3); //0 1 2 13 var c=fun(0).fun(1); c.fun(2);c.fun(3); //0 1 1
-----------------------------------------------------------------------小二给我来二两轮子!