日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js中类型识别的方法

發布時間:2024/1/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js中类型识别的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一種方法typeof

typeof是一種運算符,它的值有以下幾種

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>ces</title> </head> <body><script>console.log(typeof "Liesbeth");//"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:"Liesbeth"});//"object" console.log(typeof function(){});//"function" console.log(typeof []);//"object" console.log(typeof new Date);//"object" console.log(typeof /[0-9]/);//'object' console.log(typeof new Number(123));//'object'function Person(){};console.log(typeof new Person);//'object'</script></body> </html> typeof

?

運行結果如下,可以看出typeof能判斷基本類型的值,其他通過new創建的類型除了function外,其他都是object,其中null也是object

第二種方法instanceof

instanceof運算符可以用來判斷某個構造函數的prototype屬性所指向的對象是否存在于另外一個要檢測對象的原型鏈上即某個對象是否是某個構造函數的實例,如下

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>ces</title> </head> <body><script>//能判別內置對象內型 console.log([] instanceof Array);//true console.log([] instanceof Object);//true console.log(/[0-9]/ instanceof RegExp);//true console.log(new String('123') instanceof String);//true console.log(function(){} instanceof Function);//true//不能判斷原始類型 console.log('123' instanceof String);//false console.log(123 instanceof Number);//false console.log(true instanceof Boolean);//false//判別自定義對象類型function Point(x,y){this.x=x;this.y=y;}function Circle(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);console.log(c instanceof Circle);//true console.log(c instanceof Point);//true</script></body> </html> instanceof

運行結果如下圖

?

由此可看出instanceof不能識別原始類型,能識別自定義對象和內置對象類型同時它們都屬于Object

?第三種方法Object.prototype.toString.call

這個方法獲取的是對象的[[Class]]屬性值,返回結果是固定的'[object'+[[class]]屬性+']',jquery中的$.type()就是利用這個屬性判斷對象類型的。

?

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

?

運行結果如下:

其中的type(c)為instanceof時創建的對象。

可以看出toString能識別原始類型和內置對象,但是不能識別自定義對象類型,另外在ie7和8中null和undefined都是返回"Object"如下

同時在IE6中字符串也會返回"Object"所以用的時候要多注意這幾個值。

第四種方法constructor

constructor 屬性返回對創建此對象的數組函數的引用,如圖所示

利用此特性我們可以判別類型,其中由于null和undefined沒有構造函數所以特殊處理,編寫函數如下

function getConstructorName(obj){return (obj===undefined||obj===null)?obj:(obj.constructor&&obj.constructor.toString().match(/function\s*([^(]*)/)[1]); }

?運行結果如下

可以看出constructor能判別原始類型(除null和undefined),內置對象和自定義對象.

參考資料:http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html其中對Object.prototype.toString解釋的清楚。

?

?

?

轉載于:https://www.cnblogs.com/liziyu91/p/5689960.html

總結

以上是生活随笔為你收集整理的js中类型识别的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。