zoukankan      html  css  js  c++  java
  • 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="###" onclick="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 nihao');  

     

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

     

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

     

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

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

     

    很多题目未完待续

  • 相关阅读:
    angular、vue使用感受
    API网关在API安全性中的作用
    分享一个国内首个企业级开源的GO语言网关--GoKu API Gateway
    几种部署Goku API Gateway的方式,最快一分钟可使用上网关
    热门开源网关的性能对比:Goku > Kong > Tyk
    如何通过网关做服务编排?
    未来实现API管理系统的几个关键词
    关于未来实现API管理系统的几个关键词
    让API实现版本管理的实践
    医疗行业如何使用API市场?
  • 原文地址:https://www.cnblogs.com/ranran/p/4380804.html
Copyright © 2011-2022 走看看