js——五种数据类型 数据类型转换(引用脚本之家) 数据类型检查 三种对象类型 两种无的数据类型

1、数据类型
1、Number:所有数字
NaN 属性是代表非数字值的特殊值。该属性用于指示某个值不是数字。可以把 Number 对象设置为该值,来指示其不是数字值
提示:请使用 isNaN() 全局函数来判断一个值是否是 NaN 值

2、String:字符串
字符串嵌套需添加转义字符使用(/’或/”)或” ’ ’ “及’ ” ” ‘

3、Boolean:布尔值
true:非零、非空
false:零、空、NaN、null、undefined
(注:undefined表示变量未赋值;null表示变量值为空,可以用null来清空变量)

4、Array:数组
[” “,” “,….]

5、Object:对象
{“name”:”value”,’age’:19….}

2、数据类型转换
转数字
1、Number转换

var a = "123";
a = Number(a);

注意:

a) 如果转换的内容本身就是一个数值类型的字符串,那么将来在转换的时候会返回自己。

b) 如果转换的内容本身不是一个数值类型的字符串,那么在转换的时候结果是NaN.

c) 如果要转换的内容是空的字符串,那以转换的结果是0.

d) 如果是其它的字符,那么将来在转换的时候结果是NaN.

2、parseInt():

var a = "123";
 a = parseInt(a);

a) 忽略字符串前面的空格,直至找到第一个非空字符,还会将数字后面的非数字的字符串去掉。

b) 如果第一个字符不是数字符号或者负号,返回NaN

c) 会将小数取整。(向下取整)

3、parseFloat();//浮点数(小数)

与parseInt一样,唯一区别是parseFloat可以保留小数。

4、隐式转换

var a = "123";
a = +a;

加减乘除以及最余都可以让字符串隐式转换成number.

转字符串
1、String():

var a = 123;
a = String(a);

2、toString()的方法来进行转换(包装类)。

var a = 123; a = a.toString(); //undefined,null不能用toString。

3、隐式转换

var a = 123;
a = a + "";

转布尔类型
1、Boolean()将其它类型转为boolean值

var a ="true";
 a = Boolean(a);

注意:在进行boolean转换的时候所有的内容在转换以后结果都是true,除了:false、”“(空字符串)、0、NaN、undefined

2、隐式转换

var a = 123;
a = !!a;

3、数据类型检查
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”;}; 

1、typeof
typeof 2:number
typeof null:object
typeof {}:object
typeof []:object
typeof (function(){}):function
typeof undefined:undefined
typeof ‘222’ :string
typeof true:boolean

2、instanceof
alert(c instanceof Array): true
alert(d instanceof Date):true
alert(f instanceof Function):true
alert(f instanceof function): false

3、constructor
alert(c.constructor === Array) ———-> true
alert(d.constructor === Date) ———–> true
alert(f.constructor === Function) ——-> true
注意: constructor 在类继承时会出错
例如:function A(){};
function B(){};
A.prototype = new B(); //A继承自B
var aobj = new A();
alert(aobj.constructor === B) ———–> true;
alert(aobj.constructor === A) ———–> false;
解决方法:
1、用instanceof
2、解决construtor的问题通常是让对象的constructor手动指向自己
aobj.constructor = A; //将自己的类赋值给对象的constructor属性
alert(aobj.constructor === A) ———–> true;
alert(aobj.constructor === B) ———–> false; //基类不会报true了;

4、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;

5、jquery.type()(万能方法)
如果对象是undefined或null,则返回相应的“undefined”或“null”。
jQuery.type( undefined ) === “undefined”
jQuery.type() === “undefined”
jQuery.type( window.notDefined ) === “undefined”
jQuery.type( null ) === “null”
如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字。 (有关此技术的更多细节。 )
jQuery.type( true ) === “boolean”
jQuery.type( 3 ) === “number”
jQuery.type( “test” ) === “string”
jQuery.type( function(){} ) === “function”
jQuery.type( [] ) === “array”
jQuery.type( new Date() ) === “date”
jQuery.type( new Error() ) === “error” // as of jQuery 1.9
jQuery.type( /test/ ) === “regexp”
其他一切都将返回它的类型“object”。

通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,实在没辙就使用$.type()方法。

三种对象类型
Object、Date、Array —— object

两种无的数据类型
null —— object
undefined —— undefined

猜你喜欢

转载自blog.csdn.net/lucky541788/article/details/81667314
今日推荐