javascript 判斷各種資料的型別

liaohui5發表於2019-03-05
export default {
    isString(value) {
        return typeof value === "string";
    },

    isNumber(value) {
        return typeof value === "number";
    },

    isNaN(value) {
        return typeof value === "number" && value.toString() === "NaN";
    },

    isInt(value) {
        return this.isNumber(value) && (value % 1 === 0);
    },

    isFloat(value) {
        return this.isNumber(value) && (value % 1 !== 0);
    },

    isBool(value) {
        return typeof value === "boolean";
    },

    isUndefined(value) {
        return typeof value === "undefined";
    },

    isFunction(value) {
        return typeof value === "function";
    },

    isArray(value) {
        return Object.prototype.toString.call(value) === '[object Array]';
    },

    isObject(value) {
        return Object.prototype.toString.call(value) === '[object Object]';
    },

    isNull(value) {
        return Object.prototype.toString.call(value) === '[object Null]';
    },

};

複製程式碼

相關文章