原始值和引用值

類(lèi)型 原始值(String,Number,Boolean,null,undefined,Symbol) 引用值(Object,Array,F(xiàn)unction)
存儲(chǔ) 棧內(nèi)存中,占用固定大小空間 堆內(nèi)存中
復(fù)制區(qū)別 獨(dú)立拷貝,不會(huì)相互影響 共享對(duì)象,新舊變量會(huì)指向同一個(gè)對(duì)象
動(dòng)態(tài)修改 無(wú)法修改 可以隨時(shí)修改,添加,刪除
類(lèi)型檢查 typeof instanceof

類(lèi)型檢測(cè)typeof

主要用于原始數(shù)據(jù)類(lèi)型(string, number, boolean, undefined, symbol)檢測(cè)

// 原始類(lèi)型 
// undefined
console.log(typeof undefined);           // "undefined"
console.log(typeof undeclaredVariable);  // "undefined"

// boolean
console.log(typeof true);                // "boolean"
console.log(typeof false);               // "boolean"
console.log(typeof Boolean(1));          // "boolean"

// number
console.log(typeof 42);                  // "number"
console.log(typeof 3.14);                // "number"
console.log(typeof NaN);                 // "number"
console.log(typeof Infinity);            // "number"
console.log(typeof -Infinity);           // "number"

// string
console.log(typeof "hello");             // "string"
console.log(typeof 'world');             // "string"
console.log(typeof `template`);          // "string"

// bigint
console.log(typeof 123n);                // "bigint"
console.log(typeof BigInt(123));         // "bigint"

// symbol
console.log(typeof Symbol());            // "symbol"
console.log(typeof Symbol('description')); // "symbol"

// object
console.log(typeof {});                  // "object"
console.log(typeof []);                  // "object" (注意:數(shù)組也是對(duì)象)
console.log(typeof null);                // "object" (這是JavaScript的一個(gè)bug)
console.log(typeof new Date());          // "object"
console.log(typeof new RegExp());        // "object"
console.log(typeof new Error());         // "object"

// function
console.log(typeof function() {});       // "function"
console.log(typeof () => {});            // "function"
console.log(typeof class {});            // "function"


類(lèi)型檢測(cè)instanceof

檢查對(duì)象是否是某個(gè)構(gòu)造函數(shù)的實(shí)例

// 原始類(lèi)型 
console.log("hello" instanceof String);      // false
console.log(42 instanceof Number);           // false
console.log(true instanceof Boolean);        // false

// 檢查數(shù)組
const arr = [1, 2, 3];
console.log(arr instanceof Array);        // true
console.log(arr instanceof Object);      // true (數(shù)組也是對(duì)象)

// 檢查字符串
const str = "hello";
console.log(str instanceof String);      // false (原始類(lèi)型)
console.log(new String("hello") instanceof String); // true

// 檢查數(shù)字
const num = 42;
console.log(num instanceof Number);      // false (原始類(lèi)型)
console.log(new Number(42) instanceof Number); // true

// 檢查布爾值
const bool = true;
console.log(bool instanceof Boolean);    // false (原始類(lèi)型)
console.log(new Boolean(true) instanceof Boolean); // true

//檢查函數(shù)類(lèi)型
function myFunction() {}
const arrowFunction = () => {};

console.log(myFunction instanceof Function);      // true
console.log(arrowFunction instanceof Function);   // true
console.log(Array instanceof Function);           // true

項(xiàng)目常用寫(xiě)法


function getTypeInfo(value) {
    const type = typeof value;
    
    switch (type) {
        case 'undefined':
            return '未定義的值';
        case 'boolean':
            return '布爾值';
        case 'number':
            return isNaN(value) ? '非數(shù)字' : '數(shù)字';
        case 'string':
            return '字符串';
        case 'bigint':
            return '大整數(shù)';
        case 'symbol':
            return '符號(hào)';
        case 'object':
            if (value === null) return '空值';
            if (Array.isArray(value)) return '數(shù)組';
            if (value instanceof Date) return '日期';
            if (value instanceof RegExp) return '正則表達(dá)式';
            return '對(duì)象';
        case 'function':
            return '函數(shù)';
        default:
            return '未知類(lèi)型';
    }
}

console.log(getTypeInfo(42));            // "數(shù)字"
console.log(getTypeInfo("hello"));       // "字符串"
console.log(getTypeInfo([]));            // "數(shù)組"
console.log(getTypeInfo(null));          // "空值"
console.log(getTypeInfo(undefined));     // "未定義的值"


function validateParameters(name, age, isActive) {
    const errors = [];
    
    if (typeof name !== 'string') {
        errors.push('姓名必須是字符串');
    }
    
    if (typeof age !== 'number' || isNaN(age)) {
        errors.push('年齡必須是有效數(shù)字');
    }
    
    if (typeof isActive !== 'boolean') {
        errors.push('激活狀態(tài)必須是布爾值');
    }
    
    return errors;
}

console.log(validateParameters("張三", 25, true));     // []
console.log(validateParameters(123, "25", "true")); 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • """1.個(gè)性化消息: 將用戶的姓名存到一個(gè)變量中,并向該用戶顯示一條消息。顯示的消息應(yīng)非常簡(jiǎn)單,如“Hello ...
    她即我命閱讀 5,000評(píng)論 0 6
  • 為了讓我有一個(gè)更快速、更精彩、更輝煌的成長(zhǎng),我將開(kāi)始這段刻骨銘心的自我蛻變之旅!從今天開(kāi)始,我將每天堅(jiān)持閱...
    李薇帆閱讀 2,241評(píng)論 1 4
  • 似乎最近一直都在路上,每次出來(lái)走的時(shí)候感受都會(huì)很不一樣。 1、感恩一直遇到好心人,很幸運(yùn)。在路上總是...
    時(shí)間里的花Lily閱讀 1,737評(píng)論 1 3
  • 1、expected an indented block 冒號(hào)后面是要寫(xiě)上一定的內(nèi)容的(新手容易遺忘這一點(diǎn)); 縮...
    庵下桃花仙閱讀 1,075評(píng)論 1 2
  • 一、工具箱(多種工具共用一個(gè)快捷鍵的可同時(shí)按【Shift】加此快捷鍵選取)矩形、橢圓選框工具 【M】移動(dòng)工具 【V...
    墨雅丫閱讀 1,511評(píng)論 0 0

友情鏈接更多精彩內(nèi)容