第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

第 39 題:JS 數(shù)據(jù)類型有哪些?

標簽:
Html5 CSS3 面試

数据类型

基本类型

String、Number、Boolean、Null、Undefined

引用类型

Object、Array、Function

判断数据类型的方式

1. 使用 typeof

typeof 'test'          // string
typeof 1880            // number
typeof true            // boolean
typeof null            // object
typeof undefined       // undefined
typeof {}              // object
typeof []              // object
typeof function() {}   // function

缺点:判断基本类型还是可以的,判断引用类型作用就没那么大了。无法校验数组、对象、Null

2. 使用 constructor

let xiaoming = 'text';
xiaoming.constructor   // String(){}

let xiaoming = 1880;
xiaoming.constructor   // Number(){}

let xiaoming = true;
xiaoming.constructor   // Boolean(){}

let xiaoming = null;
xiaoming.constructor   // 报错

let xiaoming = undefined;
xiaoming.constructor   // 报错

let xiaoming = {};
xiaoming.constructor   // Object(){}

let xiaoming = [];
xiaoming.constructor   // Array(){}

let xiaoming = function() {};
xiaoming.constructor   // Function(){}

缺点:无法校验 Null 和 Undefined

3. 使用 instanceof

function A() {}

function A1() {}

function B() {}

A1.prototype = new A(); // A1 继承 A

let xiaoming = new A1(); // 实例化

console.log(xiaoming instanceof A); // true
console.log(xiaoming instanceof A1); // true
console.log(xiaoming instanceof B); // false

缺点:只适用于判断对象属于什么类型,其他的都不太适用

4. 使用 Object.prototype.toString.call()

Object.prototype.toString.call('text');            // [object String]
Object.prototype.toString.call(1880);              // [object Number]
Object.prototype.toString.call(true);              // [object Boolean]
Object.prototype.toString.call(null);              // [object Null]
Object.prototype.toString.call(undefined);         // [object Undefined]
Object.prototype.toString.call({});                // [object Object]
Object.prototype.toString.call([]);                // [object Array]
Object.prototype.toString.call(function() {});     // [object Function]

目前比较准确的一种方式

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
Web前端工程師
手記
粉絲
6
獲贊與收藏
12

關(guān)注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消