zoukankan      html  css  js  c++  java
  • js判断数字、整数、字符串、布尔,特殊方法

    整数:

    function isInteger(obj) {
        return Math.floor(obj) === obj
    }
    isInteger(3) // true
    isInteger(3.3) // false
    isInteger('') // false
    isInteger('3') // false
    isInteger(true) // false
    isInteger([]) // false

    整数:

    function isInteger(obj) {
        return (obj | 0) === obj
    }
    isInteger(3) // true
    isInteger(3.3) // false
    isInteger('') // false
    isInteger('3') // false
    isInteger(true) // false
    isInteger([]) // false
    //这个函数很不错,效率还很高。但有个缺陷,上文提到过,位运算只能处理32位以内的数字,对于超过32位的无能为力,如
    //isInteger(Math.pow(2, 32)) // 32位以上的数字返回false了
    //当然,多数时候我们不会用到那么大的数字。

    原:http://www.cnblogs.com/snandy/p/3824828.html

    数字:

    function isNumber(obj) {
        return obj === +obj
    }

    数字(es6):

    Number.isInteger(3) // true

    字符串和布尔:

    // 判断字符串
    function isString(obj) {
        return obj === obj+''
    }
    // 判断布尔类型
    function isBoolean(obj) {
        return obj === !!obj
    }
  • 相关阅读:
    python 中多个装饰器的执行顺序:
    Python基础思维导图
    怎样写出靠谱的RESUTful API接口?
    python中yield()的用法详解
    Flask思维导图
    Django的设计模式
    MySQL
    MySQL
    Linux
    zsh oh-my-zsh 插件推荐
  • 原文地址:https://www.cnblogs.com/daysme/p/6547857.html
Copyright © 2011-2022 走看看