js推斷數據類型

一、JS中數據範例

  • 基礎數據範例(Undefined、Null、Boolean、Number、String)
  • 龐雜數據範例 (Object)

二、推斷數據範例

下面將對以下數據舉行推斷它們的範例

var bool = true
var num = 1
var str = 'abc'
var und = undefined
var nul = null
var arr = [1,2,3]
var obj = {name:'haoxl',age:18}
var fun = function(){console.log('I am a function')}

1.運用typeof

console.log(typeof bool); //boolean
console.log(typeof num);//number
console.log(typeof str);//string
console.log(typeof und);//undefined
console.log(typeof nul);//object
console.log(typeof arr);//object
console.log(typeof obj);//object
console.log(typeof fun);//function

由效果可知typeof能夠測試出
number
string
boolean
undefined
function,而關於
null及數組、對象,typeof均檢測出為object,不能進一步推斷它們的範例。

2.運用instanceof

console.log(bool instanceof Boolean);// false
console.log(num instanceof Number);// false
console.log(str instanceof String);// false
console.log(und instanceof Object);// false
console.log(arr instanceof Array);// true
console.log(nul instanceof Object);// false
console.log(obj instanceof Object);// true
console.log(fun instanceof Function);// true

var bool2 = new Boolean()
console.log(bool2 instanceof Boolean);// true

var num2 = new Number()
console.log(num2 instanceof Number);// true

var str2 = new String()
console.log(str2 instanceof String);//  true

function Person(){}
var per = new Person()
console.log(per instanceof Person);// true

function Student(){}
Student.prototype = new Person()
var haoxl = new Student()
console.log(haoxl instanceof Student);// true
console.log(haoxl instanceof Person);// true

從效果中看出instanceof不能區分undefined和null,而且關於基礎範例假如不是用new聲明的則也測試不出來,關因而運用new聲明的範例,它還能夠檢測出多層繼續關聯。

3.運用constructor

undefined和null沒有contructor屬性

console.log(bool.constructor === Boolean);// true
console.log(num.constructor === Number);// true
console.log(str.constructor === String);// true
console.log(arr.constructor === Array);// true
console.log(obj.constructor === Object);// true
console.log(fun.constructor === Function);// true

console.log(haoxl.constructor === Student);// false
console.log(haoxl.constructor === Person);// true

constructor不能推斷undefined和null,而且運用它是不安全的,由於contructor的指向是能夠轉變的

4.運用Object.prototype.toString.call

console.log(Object.prototype.toString.call(bool));//[object Boolean]
console.log(Object.prototype.toString.call(num));//[object Number]
console.log(Object.prototype.toString.call(str));//[object String]
console.log(Object.prototype.toString.call(und));//[object Undefined]
console.log(Object.prototype.toString.call(nul));//[object Null]
console.log(Object.prototype.toString.call(arr));//[object Array]
console.log(Object.prototype.toString.call(obj));//[object Object]
console.log(Object.prototype.toString.call(fun));//[object Function]

function Person(){}
function Student(){}
Student.prototype = new Person()
var haoxl = new Student()
console.log(Object.prototype.toString.call(haoxl));//[object Object]

道理(摘自高等程序設計3):在任何值上挪用 Object 原生的 toString() 要領,都邑返回一個 [object NativeConstructorName] 花樣的字符串。每一個類在內部都有一個 [[Class]] 屬性,這個屬性中就指定了上述字符串中的組織函數名。

然則它不能檢測非原生組織函數的組織函數名。

5.運用jquery中的$.type

console.log($.type(bool));//boolean
console.log($.type(num));//number
console.log($.type(str));//string
console.log($.type(und));//undefined
console.log($.type(nul));//null
console.log($.type(arr));//array
console.log($.type(obj));//object
console.log($.type(fun));//function

function Person(){}
function Student(){}
Student.prototype = new Person()
var haoxl = new Student()
console.log($.type(haoxl));//object

$.type()內部道理就是用的Object.prototype.toString.call()

    原文作者:依晨
    原文地址: https://segmentfault.com/a/1190000015264821
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞