var num = 123;var str = 'abcdef';var bool = true;var arr = [1, 2, 3, 4];var json = {name:'wenzi', age:25};var func = function(){ console.log('this is function'); }var und = undefined;var nul = null;var date = new Date();var reg = /^[a-zA-Z]{5,20}$/;var error= new Error();console.log(typeof num,typeof str,typeof bool,typeof arr,typeof json,typeof func,typeof und,typeof nul,typeof date,typeof reg,typeof error);试下看看。
判断js中的数据类型有一下几种方法:typeof、instanceof、constructor、prototype、$.type()/jquery.type(),接下来主要比较一下这几种方法的异同。
1、最常见的判断方法:typeof:
2、判断已知对象类型的方法: instanceof:
3、根据对象的constructor判断: constructor:
4、通用但很繁琐的方法: prototype:
5、无敌万能的方法:jquery.type():
通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,实在没辙就使用$.type()方法。
去百度文库,查看完整内容>内容来自用户:张臣js类型识别是一个简单而又纠结的问题。
typeof操作符可以说是js中最简单直接的类型判断方式了,缺点就是有时也不太靠谱。typeof[];// -> 'object'typeofnull;// -> 'object'它无法识别Array类型和null类型,还有Date、Math、Error、JSON、RegExp这些类型就更不必多说了。
instanceof操作符能判断一个对象是否是某个类(或子类)的一个实例。vararr=[1,2,3];arrinstanceofArray;// -> ;// -> true因为Array是Object的子类相比typeof,instanceof的范围广泛很多,还可以判断自定义类型。
如:functionAnimal(){}varcat=newAnimal();catinstanceofAnimal;// -> true对于string、boolean、number等原始值类型,instanceof就不太好用了:3instanceofNumber;// -> ;// -> false'abc'instanceofString;// -> false因为原始值类型并不是任何类的实例,还记得以前提到过的String()以及new String()的区别吧。constructor能反映创建当前对象的构造函数,也没有如instanceof那样无法正确识别string等原始值类型:(3).constructor===Number// -> truetrue.constructor===Boolean// -> true'abc'.constructor===String// -> true因为对于string、boolean、number等原始值类型在访问其属性的时候,会先创建对应的数据类型的临时实例,访问的是这个临时实例的方法。
判断js中的数据类型有一下几种方法:typeof、instanceof、constructor、prototype、$.type()/jquery.type(),接下来主要比较一下这几种方法的异同。
1、最常见的判断方法:typeof: 2、判断已知对象类型的方法: instanceof: 3、根据对象的constructor判断: constructor: 4、通用但很繁琐的方法: prototype: 5、无敌万能的方法:jquery.type(): 通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,实在没辙就使用$.type()方法。
可以参考下面的两种方法:
1、直接判断对象不为null
if (!myObj) {
var myObj = { };
}
Javascript语言是"先解析,后运行",解析时就已经完成了变量声明
2、使用window对象判断某对象是否存在
if (!window.myObj) {
var myObj = { };
}
扩展资料:
javascript函数
charCodeAt(index)返回一个整数,该整数表现String对象中指定位置处的字符的Unicode编码
concat(string2)衔接两条或少条字符串
fromCharCode(num1, num2, …,BB霜, numN)获取指定的Unicode值并返回字符串
indexOf(searchString, startIndex) 返回字符串中第一个呈现指定字符串的地位
lastlndexOf(searchString, startIndex) 返回字符串中最后一个呈现指定字符串的地位
match(regex) 在字符串中查觅指定值
参考资料来源:百度百科-javascript
参考资料来源:百度百科-JavaScript 函数
如何判断js中的数据类型:typeof、instanceof、constructor、prototype方法比较如何判断js中的类型呢,先举几个例子:var a = "iamstring.";var b = 222;var c= [1,2,3];var d = new Date();var e =function(){alert(111);};var f =function(){this.name="22";};最常见的判断方法:typeofalert(typeof a) ------------> stringalert(typeof b) ------------> numberalert(typeof c) ------------> objectalert(typeof d) ------------> objectalert(typeof e) ------------> functionalert(typeof f) ------------> function其中typeof返回的类型都是字符串形式,需注意,例如:alert(typeof a == "string")-------------> truealert(typeof a == String)---------------> false另外typeof可以判断function的类型;在判断除Object类型的对象时比较方便。
判断已知对象类型的方法: instanceofalert(c instanceof Array)---------------> truealert(d instanceofDate) alert(f instanceof Function)------------> truealert(f instanceof function)------------> false注意:instanceof后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。根据对象的constructor判断:constructoralert(c.constructor ===Array) ----------> truealert(d.constructor === Date)-----------> truealert(e.constructor ===Function) -------> true注意: constructor 在类继承时会出错eg,function A(){};function B(){};A.prototype = new B(); //A继承自Bvar aObj = new A();alert(aobj.constructor === B) ----------->true;alert(aobj.constructor === A) ----------->false;而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:alert(aobj instanceof B) ---------------->true;alert(aobj instanceof B) ---------------->true;言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:aobj.constructor = A;//将自己的类赋值给对象的constructor属性alert(aobj.constructor === A) ----------->true;alert(aobj.constructor === B) ----------->false; //基类不会报true了;通用但很繁琐的方法: prototypealert(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;大小写不能写错,比较麻烦,但胜在通用。
通常情况下用typeof判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,简单总结下,挖个坑,欢迎补充。
alert(typeof a) ------------> string
alert(typeof b) ------------> number
alert(typeof c) ------------> object
alert(typeof d) ------------> object
alert(typeof e) ------------> function
alert(typeof f) ------------> function
其中typeof返回的类型都是字符串形式,需注意,例如:
alert(typeof a == "string") -------------> true
alert(typeof a == String) ---------------> false
另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
蜀ICP备2020033479号-4 Copyright © 2016 学习鸟. 页面生成时间:2.976秒