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代码
- var a = 'nihao';
- var b = 222;
- var c = [1,2,3];
- var d = new Date();
- var e = function(){alert('hanshu');};
- var f = function(){this.name = 'hanmeimei'};
- alert(typeof a);//string
- alert(typeof a == String);// false
- alert(typeof b);// number
- alert(typeof c);// object
- alert(typeof d);// object
- alert(typeof e);// function
- alert(typeof f);// function
- alert(c instanceof Array);//true
- alert(e instanceof Function);//true
- alert(c.constructor === Array);//true
- function A(){};
- function B(){};
- A.prototype = new B(); //A继承自B注意: constructor 在类继承时会出错
- var aObj = new A();
- alert(aObj.constructor === B);// -----------> true;
- alert(aObj.constructor === A);// -----------> false;
- //而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
- alert(aObj instanceof B); //----------------> true;
- alert(aObj instanceof A); //----------------> true;
- //解决construtor的问题通常是让对象的constructor手动指向自己:
- aObj.constructor = A;//将自己的类赋值给对象的constructor属性
- alert(aObj.constructor === B);// -----------> flase;
- alert(aObj.constructor === A);//true
- //prototype
- alert(Object.prototype.toString.call(a) === '[object String]');//true;
- alert(Object.prototype.toString.call(b) === '[object Number]');//true;
- alert(Object.prototype.toString.call(c) === '[object Array]');//true;
- alert(Object.prototype.toString.call(d) === '[object Date]');//true;
- alert(Object.prototype.toString.call(e) === '[object Function]');//true;
- alert(Object.prototype.toString.call(f) === '[object Function]');//true;
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:js简单的面试题
关键词:JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。