前端笔试题 JS部分
题目 http://www.itmian4.com/forum.php?mod=viewthread&tid=4540
http://www.itmian4.com/forum.php?mod=viewthread&tid=4540 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script type="text/javascript"> var div=document.createElement('div'); var o={a:1,b:{c:1}}; var obj = { name: "Simon", age: "20", clothing: { style: "simple", isDouche: false } } var arr=[1,2,3]; window.onload=function(){ //当输入框获取焦点时 删除原有字符 离开输入框且未输入字符时重新显示原有字符 //checkInput(); //当点击链接/按钮时 显示这是当前第几个链接/按钮 //countLink(); //countBtn(); //countBtn2(); //获取全部父节点名字 //getAllParents(document.getElementById('getParent')); // 给定一个对象var o={a:1,b:{c:1}}; 取到全部属性的名字 如abc //recursion(o); //recursion(obj); //比较除了第一个字符之外剩余字串 并按顺序排列 //asort(); //为数组添加Shuffle()方法 // addShuffle(); // testShuffle(); // 将两个数组合并在一起 //mergeArray(); // 判断是不是数组 //isArray(arr); //alertMath(); //数组去重 //deleteDupligate(); //addUniqueForArray(); // 判断是否是String类型 'xxx' new String('xxx') 都要能够正确判断 //testString(); //显示标签名称 //alertName(); } // all props function allProp(){ for(prop in div.style){ str+=(prop+=' '); } document.getElementById('msg').innerHTML=str; } function checkProp(prop){ if(prop in div.style){ return true; }else{ return false; } } function checkInput(){ var input=document.getElementsByName('input1')[0]; //下面这种定义事件的方式看起来简单但是不好 //看起来定义了为blur事件定义了两个函数 //实际上后面一个函数覆盖了前面一个 最终只会弹出b2 // input.onblur=function(){ // alert('blur'); // } // input.onblur=function(){ // alert('b2'); // } //所以应该使用下面的方式 input.addEventListener('focus',function(){ if(this.value=='default'){ this.value=''; } },false); input.addEventListener('blur',function(){ if(this.value==''){ this.value='default'; } }); } function countLink(){ var sum=document.getElementsByTagName('a').length; console.log(sum); for (var i = 0; i < sum; i++) { document.getElementsByTagName('a')[i].onclick=function(x){ return function(){ alert(x); } }(i); //使用闭包Closure的方式向匿名函数传参 } } //关于事件传参使用Closure http://stackoverflow.com/questions/10856517/javascript-issue-with-scope-and-passing-parameters-to-dynamically-created-even function countBtn(){ var all=document.getElementsByTagName('button'); var sum=document.getElementsByTagName('button').length; for (var i = 0; i < all.length; i++) { all[i].addEventListener('click',function(x) { return function(){ alert(x); } }(i)); } } // 为什么要用return check this // http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example function countBtn2(){ var all=document.getElementsByTagName('button'); for (var i = 0; i < all.length; i++) { all[i].addEventListener('click',closureForCountBtn2(i)); } } function closureForCountBtn2(x){ return function(){ alert(x); } } function getAllParents(dom){ if (dom.tagName=='html') { console.log('html it is the root'); }else{ console.log(dom.tagName); getAllParents(dom.parentNode) } } function recursion(o){ for(var name in o){ if(o.hasOwnProperty(name)){ console.log(name+" "+typeof(o[name])+" "+(o[name] instanceof Object)); if(o[name] instanceof Object){ recursion(o[name]); } } } } function asort(){ var arr=["abd","cba","ba"]; for (var i = 0; i < arr.length; i++) { for (var j = 0; j < arr.length-i-1; j++) { //if(strcmp(arr[j],arr[j+1])==1){ //现在的要求是只比较除了第一个字符之外的剩余字符 if(strcmp(arr[j].substring(1),arr[j+1].substring(1))){ temp=arr[j+1]; arr[j+1]=arr[j]; arr[j]=temp; } }; }; for(var index in arr){ console.log(arr[index]); } } function strcmp(str1,str2){ return ( str1 < str2 ) ? -1 : ( str1 > str2 ? 1 : 0 ); } function addShuffle(){ Array.prototype.shuffle=function(){ var len=this.length; for(var i=0,j=0;i<len;i++){ j=(function(x){ return Math.floor(Math.random()*x); }(len)); temp=this[i]; this[i]=this[j]; this[j]=temp; } return this; } } function getRandom(len){ return Math.floor(Math.random()*len); } function testShuffle(){ var arr=[1,2,3,4,5,6,7,8,9,10]; arr.shuffle(); console.log(arr); } function mergeArray(){ var a = [-3,-1,0,1,3,5,7,9]; var b = [-4,-2,0,2,3,4,5,6,7,8]; for (p in b) { a.push(p); } a.sort(); console.log(a); } function isArray(obj){ //alert(obj instanceof Array); //alert(obj.constructor==Array); //constructor console.log('constructor---------------'); console.log(Object.constructor);//function Function() { [native code] } console.log(typeof Object);//function 可执行对象说一律返回function console.log(Object instanceof Object);//true console.log(typeof Object.constructor);//function console.log(Array.constructor);//function Function() { [native code] } console.log({}.constructor);//function Object() { [native code] } console.log([].constructor);//function Array() { [native code] } //constructor == var arr=[1,2,3]; console.log(arr.constructor);//function Array() { [native code] } console.log(arr.constructor==Array);//true //prototype console.log('---------------'); console.log(Object.prototype);//Object {} console.log(Object.prototype.constructor);//function Object() { [native code] } console.log({}.prototype);//undefined //根据犀牛书121页 字面量对象的原型就是Object.prototype var F=function(){}; console.log(F); console.log(F.prototype.constructor);//function (){} console.log(F.prototype);//Object {} //由此可见 函数的prototye属性是一个对象 console.log(typeof F.prototype); console.log((function(){}).prototype);//Object {} console.log((function(){}).prototype.constructor);//function (){} // //console.log(arr.prototype.constructor);//Uncaught TypeError: Cannot read property 'constructor' of undefined //console.log({}.prototype.constructor);//Uncaught TypeError: Cannot read property 'constructor' of undefined //var o1= Object.create([1,2,3]); // console.log(o1.prototype);//undefined console.log('----------------'); function Animal(name){ } console.log(typeof Animal.prototype);//object console.log(Animal.prototype);//Animal {} } function alertMath(){ var a= (Math.PI++); var b = (Math.PI++); alert(a);//3.14... alert(b); } function deleteDupligate(){ var arr=[1,1,2,3,4,4,5,6,7,7,8,9]; for (var i = 1; i < arr.length; i++) { if(isExists(i,arr)){ mymove(i,arr); arr.length=arr.length-1; } } console.log(arr); } function isExists(i,arr){ flag=false; for (var j = 0; j < i; j++) { if(arr[i]==arr[j]){ flag=true; break; } } return flag; } function mymove(i,arr){ for(j=i;j<arr.length-1;j++){ arr[j]=arr[j+1]; } } /*上面去除重复元素的方法太C语言了 */ // 看看这个帖子 http://php.js.cn/blog/array-unique-in-javascript/ //原文中第4中实现似乎是最高效的方式 但是改变了元素顺序 所以还是写第2种好了 function addUniqueForArray(){ //为数组添加function Array.prototype.unique4=function(){ var uni={};//不重复元素列表 //key-value形式 var re=[]; for (var i = 0; i < this.length; i++) { if(!uni[this[i]]){ uni[this[i]]=true;//值设置为true re.push(this[i]); } }; return r; } } function classof(obj){ if(obj===null)return null; if(obj===undefined) return undefined; // 注意 null undefined在使用==的时候被认为是相同的 //所以 第一个判断上如果使用的是== 是无法判断究竟是null还是undefined return Object.prototype.toString.call(obj).slice(8,-1); } function testString(){ var isString=function(str){ return (typeof str=='string');//这种方式不能检测包装对象 } console.log(isString('sdcds'));//true console.log(isString(new String('scsc')));//false var isString2=function(arg){ return ((typeof arg=='string')||(arg.constructor==String)); } console.log(isString2(new String('')));//true var isString3=function(arg){ //return ((typeof arg=='string')||(typeof classof(arg)=='string')); return ((typeof arg=='string')||(classof(arg)=='String')); } console.log(isString3(new String('')));//true } function alertName(){ var all=document.getElementsByTagName('a'); for (var i = 0; i < all.length; i++) { console.log(all[i].tagName); }; } // 异步加载script function loadScript(url, callback) { var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState){//IE script.onreadystatechange = function(){ if (script.readyState == "loaded"||script.readyState == "complete"){ script.onreadystatechange =null; callback(); } }; }else{//Others: Firefox, Safari, Chrome,and Opera script.onload = function() { callback(); }; } script.src = url; document.body.appendChild(script); } </script> </head> <body> <div class='root'> <a href="javascript:void(0);">111</a> <button>1</button> <div> <a href="javascript:void(0);">222</a> <button> <span id='getParent'>2</span> </button> <div> <a href="javascript:void(0);">323</a> <button>3</button> </div> </div> </div> </body> </html>