zoukankan      html  css  js  c++  java
  • 《Javascript秘密花园》学习笔记(终)

    实践了许多代码后,回头再看《Javascript秘密花园》,又有一些收获,与诸君分享

    数组

    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
    

    可以看出,typeof运算符主要有这些值:object,string,number,function.使用new关键字创建的对象除去function外typeof运算符都会得到object.但在判断string和number之间很好用,尤其是数组中的元素是否为string或number

    //给出一个数组
    var test = [1,'1',true,new Function(""),'asd',function(){}];
    typeof test[0]//number
    typeof test[1]//string
    typeof test[2]//boolean
    typeof test[3]//function
    typeof test[4]//string
    typeof test[5]//function

    由上段代码可以看出typeof运算符在对非new关键字创建的元素类型【尤其是数组元素类型】判断上很好用。注意1和‘1’的判断。
    所以要判断数组中的元素是否为数值类型(Number),可以这样写:

    var test = [12,'123',123,'2222','agc'];
    for(var i=0,len=test.length;i<len;i++){
        console.log(typeof test[i])
    }
  • 相关阅读:
    机器学习-决策树
    第八章- 假设检验
    第七章-参数估计
    第六章-总体与样本
    第五章-大数定律&中心极限定理
    第三,四章-多维随机变量及其分布
    第二章-随机变量分布
    第一章, 随机事件
    第六章-二次型
    第五章-矩阵的特征值和特征向量
  • 原文地址:https://www.cnblogs.com/xihe/p/6138618.html
Copyright © 2011-2022 走看看