js简单的面试题

王朝学院·作者佚名  2016-05-22
窄屏简体版  字體: 小  |  中  |  大  |  超大  

js简单的面试题1,js有哪些数据类型,数据类型的判断函数?

String,Number,Boolean,Null,Undefined,Object

判断函数有:typeof,instanceof,constructor,PRototype

接下来我们一一对这些进行举例子。

Js代码vara='nihao';varb=222;varc=[1,2,3];vard=newDate();vare=function(){alert('hanshu');};varf=function(){this.name='hanmeimei'};alert(typeofa);//stringalert(typeofa==String);//falsealert(typeofb);//numberalert(typeofc);//objectalert(typeofd);//objectalert(typeofe);//functionalert(typeoff);//functionalert(cinstanceofArray);//truealert(einstanceofFunction);//truealert(c.constructor===Array);//truefunctionA(){};functionB(){};A.prototype=newB();//A继承自B注意:constructor在类继承时会出错varaObj=newA();alert(aObj.constructor===B);//----------->true;alert(aObj.constructor===A);//----------->false;//而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:alert(aObjinstanceofB);//---------------->true;alert(aObjinstanceofA);//---------------->true;//解决construtor的问题通常是让对象的constructor手动指向自己:aObj.constructor=A;//将自己的类赋值给对象的constructor属性alert(aObj.constructor===B);//----------->flase;alert(aObj.constructor===A);//true//prototypealert(Object.prototype.toString.call(a)==='[objectString]');//true;alert(Object.prototype.toString.call(b)==='[objectNumber]');//true;alert(Object.prototype.toString.call(c)==='[objectArray]');//true;alert(Object.prototype.toString.call(d)==='[objectDate]');//true;alert(Object.prototype.toString.call(e)==='[objectFunction]');//true;alert(Object.prototype.toString.call(f)==='[objectFunction]');//true;

2,编写一个js函数,时时显示当前时间,格式:“年-月-日 时:分:秒”

Js代码functionnowtime(){varnowDate=newDate();varyear=nowDate.getFullYear();varmonth=nowDate.getMonth()+1;varday=nowDate.getDate();varhours=nowDate.getHours();varminutes=nowDate.getMinutes();varsecond=nowDate.getSeconds();returnyear+'-'+month+'-'+day+''+hours+':'+minutes+':'+second;}alert(nowtime());Js代码

3,显示隐藏dom元素

使用jquery

Js代码$(function(){$("#div").show();$("#div").hide();});

4,如果添加HTML元素的事件处理,几种方法

1,直接元素中添加:

Js代码<ahref="###"onclick="fn();">click</a>

2,找到dom节点如:

Js代码varob=document.getElementById("div");ob.onclick=function(){};

3,使用jquery添加静态的dom节点的事件

Js代码$("#div").click(function(){});//动态生成的节点的话:$("#div").on("click",function(){});$("#div").live("click",function(){});

5,如何控制alert中的换行

Js代码alert('nihao\nnihao');

6,判断字符串中出现次数最多的字符,统计这个次数。

7,判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母,数字,下划线,总长度为5-20

8,请编写一个javascript函数parseQueryString,他的用途是把URL参数解析为一个对象,如:

var url=“http:witmax,cn/index.php?key0=0&key1=1&key2=2”;

很多题目未完待续

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航