js简单的面试题

 1,js有哪些数据类型,数据类型的判断函数?

String,Number,Boolean,Null,Undefined,Object

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

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

Js代码   收藏代码
  1. var a = 'nihao';  
  2. var b = 222;  
  3. var c = [1,2,3];  
  4. var d = new Date();  
  5. var e = function(){alert('hanshu');};  
  6. var f = function(){this.name = 'hanmeimei'};  
  7. alert(typeof a);//string  
  8. alert(typeof a == String);// false  
  9. alert(typeof b);// number  
  10. alert(typeof c);// object  
  11. alert(typeof d);// object  
  12. alert(typeof e);// function  
  13. alert(typeof f);// function  
  14. alert(c instanceof Array);//true  
  15. alert(e instanceof Function);//true  
  16. alert(c.constructor === Array);//true  
  17. function A(){};  
  18. function B(){};  
  19. A.prototype = new B(); //A继承自B注意: constructor 在类继承时会出错  
  20. var aObj = new A();  
  21. alert(aObj.constructor === B);// -----------> true;  
  22. alert(aObj.constructor === A);// -----------> false;  
  23. //而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:  
  24. alert(aObj instanceof B); //----------------> true;  
  25. alert(aObj instanceof A); //----------------> true;  
  26. //解决construtor的问题通常是让对象的constructor手动指向自己:  
  27. aObj.constructor = A;//将自己的类赋值给对象的constructor属性  
  28. alert(aObj.constructor === B);// -----------> flase;  
  29. alert(aObj.constructor === A);//true  
  30. //prototype  
  31. alert(Object.prototype.toString.call(a) === '[object String]');//true;  
  32. alert(Object.prototype.toString.call(b) === '[object Number]');//true;  
  33. alert(Object.prototype.toString.call(c) === '[object Array]');//true;  
  34. alert(Object.prototype.toString.call(d) === '[object Date]');//true;  
  35. alert(Object.prototype.toString.call(e) === '[object Function]');//true;  
  36. alert(Object.prototype.toString.call(f) === '[object Function]');//true;  

 

 

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

Js代码   收藏代码
  1. function nowtime(){  
  2. var nowDate = new Date();  
  3. var year = nowDate.getFullYear();  
  4. var month = nowDate.getMonth() + 1;  
  5. var day = nowDate.getDate();  
  6. var hours = nowDate.getHours();  
  7. var minutes = nowDate.getMinutes();  
  8. var second = nowDate.getSeconds();  
  9. return year + '-' + month + '-' + day +' '+hours+':'+minutes +':'+second;   
  10. }  
  11. alert(nowtime());  
Js代码   收藏代码
  1.    

 

 

3,显示隐藏dom元素

使用jquery

Js代码   收藏代码
  1. $(function(){  
  2. $("#div").show();  
  3. $("#div").hide();  
  4. });  

 

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

1,直接元素中添加:

Js代码   收藏代码
  1. <a href="###" οnclick="fn();" >click</a>  

 

2,找到dom节点如:

Js代码   收藏代码
  1. var ob = document.getElementById("div");  
  2. ob.onclick = function(){};  

 

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

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

 

5,如何控制alert中的换行

Js代码   收藏代码
  1. alert('nihao\nnihao');  

 

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

 

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

 

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

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

 

很多题目未完待续

转载于:https://www.cnblogs.com/ranran/p/4380804.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值