最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解JavaScript中的4種類型識別方法

 更新時間:2015年09月14日 11:06:50   作者:小火柴的藍色理想  
JavaScript中檢測對象類型的運算符有:typeof、instanceof,還有對象的constructor屬性: 1) typeof 運算符 typeof 是一元運算符,返回結(jié)果是一個說明運算數(shù)類型的字符串。下面由小編給大家分享JavaScript中的4種類型識別方法,需要的朋友可以參考下本文

具體內(nèi)容如下:

1.typeof

  【輸出】首字母小寫的字符串形式

  【功能】

    [a]可以識別標(biāo)準(zhǔn)類型(將Null識別為object)
    [b]不能識別具體的對象類型(Function除外)

  【實例】

console.log(typeof "jerry");//"string"
console.log(typeof 12);//"number"
console.log(typeof true);//"boolean"
console.log(typeof undefined);//"undefined"
console.log(typeof null);//"object"
console.log(typeof {name: "jerry"});//"object"
console.log(typeof function(){});//"function"
console.log(typeof []);//"object"
console.log(typeof new Date);//"object"
console.log(typeof /\d/);//"object"
function Person(){};
console.log(typeof new Person);//"object"

2.Object.prototype.toString

  【輸出】[object 數(shù)據(jù)類型]的字符串形式

  【功能】

    [a]可以識別標(biāo)準(zhǔn)類型及內(nèi)置對象類型
    [b]不能識別自定義類型

  【構(gòu)造方法】

function type(obj){
  return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
}  

  【實例1】

console.log(Object.prototype.toString.call("jerry"));//[object String]
console.log(Object.prototype.toString.call(12));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
function Person(){};
console.log(Object.prototype.toString.call(new Person));//[object Object]

  【實例2】

function type(obj){
  return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
}
console.log(type("jerry"));//"string"
console.log(type(12));//"number"
console.log(type(true));//"boolean"
console.log(type(undefined));//"undefined"
console.log(type(null));//"null"
console.log(type({name: "jerry"}));//"object"
console.log(type(function(){}));//"function"
console.log(type([]));//"array"
console.log(type(new Date));//"date"
console.log(type(/\d/));//"regexp"
function Person(){};
console.log(type(new Person));//"object"

3.constructor

  【輸出】function 數(shù)據(jù)類型(){[native code]}或者function 自定義類型(){}

  【功能】

    [a]可以識別標(biāo)準(zhǔn)類型、內(nèi)置對象類型及自定義類型
    [b]不能識別undefined、null,會報錯

  【構(gòu)造方法】

function type(obj){
  var temp = obj.constructor.toString();
  return temp.replace(/^function (\w+)\(\).+$/,'$1');
}

  【實例1】

console.log(("jerry").constructor);//function String(){[native code]}
console.log((12).constructor);//function Number(){[native code]}
console.log((true).constructor);//function Boolean(){[native code]}
//console.log((undefined).constructor);//報錯
//console.log((null).constructor);//報錯
console.log(({name: "jerry"}).constructor);//function Object(){[native code]}
console.log((function(){}).constructor);//function Function(){[native code]}
console.log(([]).constructor);//function Array(){[native code]}
console.log((new Date).constructor);//function Date(){[native code]}
console.log((/\d/).constructor);//function RegExp(){[native code]}
function Person(){};
console.log((new Person).constructor);//function Person(){}

  【實例2】

function type(obj){
  var temp = obj.constructor.toString().toLowerCase();
  return temp.replace(/^function (\w+)\(\).+$/,'$1');
}
console.log(type("jerry"));//"string"
console.log(type(12));//"number"
console.log(type(true));//"boolean"
//console.log(type(undefined));//錯誤
//console.log(type(null));//錯誤
console.log(type({name: "jerry"}));//"object"
console.log(type(function(){}));//"function"
console.log(type([]));//"array"
console.log(type(new Date));//"date"
console.log(type(/\d/));//"regexp"
function Person(){};
console.log(type(new Person));//"person"

4.instanceof

  【輸出】true或false

  【功能】

    [a]可以識別內(nèi)置對象類型、自定義類型及其父類型
    [b]不能識別標(biāo)準(zhǔn)類型,會返回false
    [c]不能識別undefined、null,會報錯

  【實例】

console.log("jerry" instanceof String);//false
console.log(12 instanceof Number);//false
console.log(true instanceof Boolean);//false
//console.log(undefined instanceof Undefined);//報錯
//console.log(null instanceof Null);//報錯
console.log({name: "jerry"} instanceof Object);//true
console.log(function(){} instanceof Function);//true
console.log([] instanceof Array);//true
console.log(new Date instanceof Date);//true
console.log(/\d/ instanceof RegExp);//true
function Person(){};
console.log(new Person instanceof Person);//true
console.log(new Person instanceof Object);//true

以上內(nèi)容就是詳解JavaScript中的4種類型識別方法,希望大家喜歡。

相關(guān)文章

最新評論

延庆县| 长白| 南阳市| 东丰县| 玉树县| 张掖市| 永济市| 和硕县| 泸溪县| 兴安盟| 彭山县| 普陀区| 重庆市| 六枝特区| 辉南县| 桂林市| 湖南省| 河源市| 稷山县| 鹿泉市| 巫山县| 成安县| 古蔺县| 红原县| 金沙县| 静宁县| 花垣县| 浮梁县| 达孜县| 雷州市| 荣成市| 宝坻区| 大庆市| 岑溪市| 温州市| 嘉黎县| 辉县市| 蛟河市| 新野县| 忻城县| 北京市|