关于javascript:JS基础数据类型总结

3次阅读

共计 2466 个字符,预计需要花费 7 分钟才能阅读完成。

总结 js 数据类型的辨认判断办法
tyoeof
instanceof
Object.prototype.toString.call
constructor
最初封装一个函数,能够判断所有的类型 *

1. 数据类型
根本类型:

Undefined 类型:该类型只有一个值,即 undefined(小写),在应用 var 申明变量然而未对其加以初始化时,这个变量的值就是 undefined。
Null 类型:该类型也只有一个值,即 null(小写),null 值示意一个空对象指针,所以用 typeof 操作符检测 null 值会返回 object 的起因。
Boolean 类型:改类型有两个值:true 和 false(小写)。
Number 类型:示意整数和浮点数
String 类型:即字符串
援用类型

Object 类型:即对象
Array 类型:数组
Date 类型:日期
RegExp 类型:正则
Function 类型
2. 类型的辨认的判断办法
(1)typeof 总结:
首先 typeof 不是办法,只是一个操作符。

能够辨认规范类型(Null 除外)
不能辨认具体的对象类型(Function 除外)
返回的值首字母都是小写!!!!!!!!

// 辨认规范类型
    typeof "jerry";      //"string"
    typeof 12;           //"number"
    typeof true;         //"boolean"
    typeof undefined;   //"undefined"
    typeof null;         //"object"
    typeof {name:"jerry"};   //"object"
    
// 辨认援用类型
    typeof function(){};  //"function"
    typeof [];            //"object"
    typeof new Date;     //"object"
    typeof /\d/;         //"object"
    // 创立一个自定义对象
    function Person(){};
    typeof new Person;      //"object"

(2)instanceof

// 可能判断援用类型
[] instanceof Array;       //true
/\d/ instanceof RegExp;    //true
new Date instanceof Date;  //true
var a = function(){}; 
a instanceof Function;     //true

// 不能判断原始类型
1 instanceof Number;          //false
"jerry" instanceof String;  //false

// 可能判断自定义对象类型及父子类型
// 自定义类型
function Person(){};
Person instanceof Function;  //true
// 父子类型
function Point(x,y){
    this.x = x;
    this.y = y;
}
function Cirele(x,y,r){Point.call(this,x,y);
    this.radius = r;
}
Circle.prototype = new Point();
Circle.prototype.constructor = Circle;
var c = new Circle(1,1,2);
c instanceof Circle  //true
c instanceof Point  //true

论断:

能够判断内置对象类型
不能判断原始类型
判断自定义对象类型
联合 1 和 3,用 instanceof 能够辨认所有的对象类型
(3)Object.prototype.toString.call

Object.prototype.toString.call("123");    //"[object String]"

// 封装函数, 并做截取
function type(obj){return Object.prototype.toString.call(obj).slice(8,-1);
}
// 测试
type("123");  //"String"

// 自定义类型
function Point(x,y){
    this.x = x;
    this.y = y;
}
// 测试
type(new Point(1,2));       //"Object"

论断:

上述封装的函数能够辨认根本类型以及援用对象类型
不能辨认自定义对象类型
(4)constructor(结构这个对象的构造函数的自身)

// 判断根本类型(根本类型也有构造函数);然而 null 和 undefined 除外,它俩没有构造函数
"jerry".constructor === String;  //true
(1).constructor ===Number;        //true

// 判断援用类型
new Date().constructor === Date;  //true
[].constructor === Array;   //true

// 判断自定义对象
function Person(name){this.name = name;}
new Person("jerry").constructor === Person; //true

// 对 constructor 判断进行办法的封装
function getConstructorName(obj){return (obj===undefined||obj===null)?obj:        
       (obj.constructor && obj.constructor.toString().match(/function\s*([^(]*)/)[1]);
}

封装的原理:

obj: 如果传入的参数是 null 或者 undefined,没有构造函数间接返回
obj.constructor 如果存在执行 && 前面的语句
obj.constructor.toString():将类型对应的构造函数转化成字符串 “Function Number(){code…}”
math(/function\s(1)/)[1]:匹配构造函数的名称,正则匹配
论断:

判断根本类型(Undefined/Null 除外)
判断援用类型
判断自定义对象类型
论断:所以能够封装一个函数 getConstructorName 判断所有类型,然而这个函数返回的除了 null 和 undefined 是小写之外,其余的首字母都是大写。


  1. (↩
正文完
 0