<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        詳解JavaScript中typeof與instanceof用法

        來源:懂視網 責編:小采 時間:2020-11-27 22:05:46
        文檔

        詳解JavaScript中typeof與instanceof用法

        詳解JavaScript中typeof與instanceof用法:今天寫JS代碼,遇到動態生成多個名稱相同的input復選按鈕 需要判斷其是否是數組,用到了if (typeof(document.MapCheckMgr.checkid)!=undefined) 以前用得少,就順便查了一下關于typeof的那些事 typeof用以獲取一個變量或者表達式的類型,type
        推薦度:
        導讀詳解JavaScript中typeof與instanceof用法:今天寫JS代碼,遇到動態生成多個名稱相同的input復選按鈕 需要判斷其是否是數組,用到了if (typeof(document.MapCheckMgr.checkid)!=undefined) 以前用得少,就順便查了一下關于typeof的那些事 typeof用以獲取一個變量或者表達式的類型,type

        今天寫JS代碼,遇到動態生成多個名稱相同的input復選按鈕

        需要判斷其是否是數組,用到了if (typeof(document.MapCheckMgr.checkid)!="undefined")

        以前用得少,就順便查了一下關于typeof的那些事

         typeof用以獲取一個變量或者表達式的類型,typeof一般只能返回如下幾個結果:

        number,boolean,string,function(函數),object(NULL,數組,對象),undefined。

        如:

        alert(typeof (123));//typeof(123)返回"number" 
        alert(typeof ("123"));//typeof("123")返回"string"

        我們可以使用typeof來獲取一個變量是否存在,如if(typeof a!="undefined"){},而不要去使用if(a)因為如果a不存在(未聲明)則會出錯,

        正因為typeof遇到null,數組,對象時都會返回object類型,所以當我們要判斷一個對象是否是數組時

        或者判斷某個變量是否是某個對象的實例則要選擇使用另一個關鍵語法instanceof

        instanceof用于判斷一個變量是否某個對象的實例,如var a=new Array();alert(a instanceof Array);會返回true,

        同時alert(a instanceof Object)也會返回true;這是因為Array是object的子類。

        再如:function test(){};var a=new test();alert(a instanceof test)會返回true。

        <script>
         var str = new String();
         function show(str1){
         if(str1 instanceof String){
         alert('1');
         }else{
         alert('0');
         }
         }
         show(str);
         str = "abccddd";
         if(typeof str=='string'){alert(str);}
         else{alert('0');}
         </script>

          關于typeof

        typeof一元運算符,用來返回操作數類型的字符串。

        typeof幾乎不可能得到它們想要的結果。typeof只有一個實際應用場景,就是用來檢測一個對象是否已經定義或者是否已經賦值。而這個應用卻不是來檢查對象的類型。

        Value Class Type
        "foo" String string
        new String("foo") String object
        1.2 Number number
        new Number(1.2) Number object
        true Boolean boolean
        new Boolean(true) Boolean object
        new Date() Date object
        new Error() Error object
        [1,2,3] Array object
        new Array(1, 2, 3) Array object
        new Function("") Function function
        /abc/g RegExp object (function in Nitro/V8)
        new RegExp("meow") RegExp object (function in Nitro/V8)
        {} Object object
        new Object() Object object

        上面表格中,Type 一列表示 typeof 操作符的運算結果??梢钥吹?,這個值在大多數情況下都返回 "object"。

        Class 一列表示對象的內部屬性 [[Class]] 的值。

        JavaScript 標準文檔中定義: [[Class]] 的值只可能是下面字符串中的一個: Arguments, Array, Boolean, Date, Error, Function, JSON, Math, Number, Object, RegExp, String.

        為了獲取對象的 [[Class]],我們需要使用定義在 Object.prototype 上的方法 toString。

        對象的類定義

        JavaScript 標準文檔只給出了一種獲取 [[Class]] 值的方法,那就是使用 Object.prototype.toString。

        function is(type, obj) {
         var clas = Object.prototype.toString.call(obj).slice(8, -1);
         return obj !== undefined && obj !== null && clas === type;
        }
        is('String', 'test'); // true
        is('String', new String('test')); // true

        上面例子中,Object.prototype.toString 方法被調用,this 被設置為了需要獲取 [[Class]] 值的對象。

        注:Object.prototype.toString 返回一種標準格式字符串,所以上例可以通過 slice 截取指定位置的字符串,如下所示:

        Object.prototype.toString.call([]) // "[object Array]"
        Object.prototype.toString.call({}) // "[object Object]"
        Object.prototype.toString.call(2) // "[object Number]"

        注:這種變化可以從 IE8 和 Firefox 4 中看出區別,如下所示:

        // IE8
        Object.prototype.toString.call(null) // "[object Object]"
        Object.prototype.toString.call(undefined) // "[object Object]"
        
        // Firefox 4
        Object.prototype.toString.call(null) // "[object Null]"
        Object.prototype.toString.call(undefined) // "[object Undefined]"

        測試為定義變量

        typeof foo !== 'undefined'

        上面代碼會檢測 foo 是否已經定義;如果沒有定義而直接使用會導致 ReferenceError 的異常。 這是 typeof 唯一有用的地方。

        結論

        為了檢測一個對象的類型,強烈推薦使用 Object.prototype.toString 方法; 因為這是唯一一個可依賴的方式。正如上面表格所示,typeof 的一些返回值在標準文檔中并未定義, 因此不同的引擎實現可能不同。

        除非為了檢測一個變量是否已經定義,我們應盡量避免使用 typeof 操作符。

        x typeof x
        undefined "undefined"
        true 或false "boolean"
        任意數字或者NaN "number"
        任意字符串 "string"
        函數對象(在ECMA-262術語中,指的是實現了[[Call]] 的對象) "function"
        任意內置對象(非函數) "object"
        數組 "obeject"
        null "object"
        宿主對象(JS引擎內置對象,而不是DOM或者其他提供的) 由編譯器各自實現的字符串,但不是"undefined","number","boolean","number","string"。
        正則表達式 各瀏覽器表現不一

        如果想將null和對象區分開,則必須針對特殊值顯式檢測。如:my_value===null。對于宿主對象來說,typeof有可能并不返回‘object',而返回字符串。但實際上客戶端js中的大多數宿主對象都是‘object'類型。對于所有內置可執行對象進行typeof運算都將返回“function”。

        // Numbers
        typeof 37 === 'number';
        typeof 3.14 === 'number';
        typeof Math.LN2 === 'number';
        typeof Infinity === 'number';
        typeof NaN === 'number'; // 盡管NaN是"Not-A-Number"的縮寫,意思是"不是一個數字"
        typeof Number(1) === 'number'; // 不要這樣使用!
        // Strings
        typeof "" === 'string';
        typeof "bla" === 'string';
        typeof (typeof 1) === 'string'; // typeof返回的肯定是一個字符串
        typeof String("abc") === 'string'; // 不要這樣使用!
        // Booleans
        typeof true === 'boolean';
        typeof false === 'boolean';
        typeof Boolean(true) === 'boolean'; // 不要這樣使用!
        // Undefined
        typeof undefined === 'undefined';
        typeof blabla === 'undefined'; // 一個未定義的變量,或者一個定義了卻未賦初值的變量
        // Objects
        typeof {a:1} === 'object';
        typeof [1, 2, 4] === 'object'; 
        // 使用Array.isArray或者Object.prototype.toString.call方法
        //可以分辨出一個數組和真實的對象
        typeof new Date() === 'object';
        typeof new Boolean(true) === 'object' // 令人困惑.不要這樣使用
        typeof new Number(1) === 'object' // 令人困惑.不要這樣使用
        typeof new String("abc") === 'object'; // 令人困惑.不要這樣使用
        // Functions
        typeof function(){} === 'function';
        typeof Math.sin === 'function';

        關于instanceof

        instanceof 左操作數是一個類,右操作數是標識對象的類。如果左側的對象是右側類的實例,則返回true.而js中對象的類是通過初始化它們的構造函數來定義的。即instanceof的右操作數應當是一個函數。所有的對象都是object的實例。如果左操作數不是對象,則返回false,如果右操作數不是函數,則拋出typeError。

        instanceof 運算符是用來測試一個對象是否在其原型鏈原型構造函數的屬性。其語法是object instanceof constructor

        instanceof 操作符用來比較兩個操作數的構造函數。只有在比較自定義的對象時才有意義。 如果用來比較內置類型,將會和 typeof 操作符 一樣用處不大。

        比較自定義對象

        function Foo() {}
        function Bar() {}
        Bar.prototype = new Foo();
        new Bar() instanceof Bar; // true
        new Bar() instanceof Foo; // true
        // 如果僅僅設置 Bar.prototype 為函數 Foo 本身,而不是 Foo 構造函數的一個實例
        Bar.prototype = Foo;
        new Bar() instanceof Foo; // false

        instanceof 比較內置類型

        new String('foo') instanceof String; // true
        new String('foo') instanceof Object; // true
        'foo' instanceof String; // false
        'foo' instanceof Object; // false

        有一點需要注意,instanceof 用來比較屬于不同 JavaScript 上下文的對象(比如,瀏覽器中不同的文檔結構)時將會出錯, 因為它們的構造函數不會是同一個對象。

        結論:instanceof 操作符應該僅僅用來比較來自同一個 JavaScript 上下文的自定義對象。 正如 typeof 操作符一樣,任何其它的用法都應該是避免的。

        function C(){} // defining a constructor
        function D(){} // defining another constructor
        var o = new C();
        o instanceof C; // true, because: Object.getPrototypeOf(o) === C.prototype
        o instanceof D; // false, because D.prototype is nowhere in o's prototype chain
        o instanceof Object; // true, because:
        C.prototype instanceof Object // true
        C.prototype = {};
        var o2 = new C();
        o2 instanceof C; // true
        o instanceof C; // false, because C.prototype is nowhere in o's prototype chain anymore
        D.prototype = new C(); // use inheritance
        var o3 = new D();
        o3 instanceof D; // true
        o3 instanceof C; // true
        var myString = new String();
        var myDate = new Date();
        myString instanceof String; // returns true
        myString instanceof Object; // returns true
        myString instanceof Date; // returns false
        myDate instanceof Date; // returns true
        myDate instanceof Object; // returns true
        myDate instanceof String; // returns false
        function Car(make, model, year) {
         this.make = make;
         this.model = model;
         this.year = year;
        }
        var mycar = new Car("Honda", "Accord", 1998);
        var a = mycar instanceof Car; // returns true
        var b = mycar instanceof Object; // returns true

        總結

        以上所述是小編給大家介紹的JavaScript中typeof與instanceof用法 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        詳解JavaScript中typeof與instanceof用法

        詳解JavaScript中typeof與instanceof用法:今天寫JS代碼,遇到動態生成多個名稱相同的input復選按鈕 需要判斷其是否是數組,用到了if (typeof(document.MapCheckMgr.checkid)!=undefined) 以前用得少,就順便查了一下關于typeof的那些事 typeof用以獲取一個變量或者表達式的類型,type
        推薦度:
        標簽: 使用 用法 js
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 日本XXX黄区免费看| 一级毛片成人免费看a| 中文字幕日本人妻久久久免费| a毛片基地免费全部视频| 国产亚洲综合一区柠檬导航| 亚洲精品无码专区在线| 免费阿v网站在线观看g| 日韩免费视频播播| 亚洲日本精品一区二区| 亚洲av无码专区首页| 91禁漫免费进入| 亚洲性久久久影院| 亚洲jizzjizz少妇| 成人毛片免费视频| 亚洲精彩视频在线观看| 亚洲色婷婷一区二区三区| 午夜亚洲WWW湿好爽| 好吊妞998视频免费观看在线| 亚洲神级电影国语版| 99久久免费精品高清特色大片| 国产成人A人亚洲精品无码| 亚洲综合在线观看视频| 亚洲一级毛片免费在线观看| 国产精品成人免费观看| 国产成人精品免费视| 久久久久亚洲AV无码网站| 久久久久久毛片免费播放| 亚洲精品私拍国产福利在线| 国产免费一区二区视频| 日韩精品福利片午夜免费观着| 日本免费人成视频播放| 亚洲男女一区二区三区| 国产特黄一级一片免费| 麻豆成人久久精品二区三区免费| 亚洲国产精品无码AAA片| 久操视频在线免费观看| 又黄又爽无遮挡免费视频| 国产亚洲蜜芽精品久久| 国产成人亚洲影院在线观看| yellow免费网站| 老司机亚洲精品影院无码 |