你的位置:首页 > 软件开发 > Java > js简单的面试题

js简单的面试题

发布时间:2015-03-31 15:00:13
1,js有哪些数据类型,数据类型的判断函数?String,Number,Boolean,Null,Undefined,Object判断函数有:typeof,instanceof,constructor,prototype接下来我们一一对这些进行举例子。Js代码 var ...

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

String,Number,Boolean,Null,Undefined,Object

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

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

Js代码  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;  

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:js简单的面试题

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。