zoukankan      html  css  js  c++  java
  • js中typeof的用法

     

    一、

    经常会在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组.

    if(document.mylist.length != "undefined" ) {} 这个用法有误.

    正确的是 if( typeof(document.mylist.length) != "undefined" ) {}

    或 if( !isNaN(document.mylist.length) ) {}

    typeof的运算数未定义,返回的就是 "undefined".

    运算数为数字 typeof(x) = "number"

    字符串 typeof(x) = "string"

    布尔值 typeof(x) = "boolean"

    对象,数组和null typeof(x) = "object"

    函数 typeof(x) = "function"

    typeof 运算符返回一个用来表示表达式的数据类型的字符串。 
    可能的字符串有:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。 
    如: 
    alert(typeof (123));//typeof(123)返回"number" 
    alert(typeof ("123"));//typeof("123")返回"string"

    二、
    typeof 运算符

    返回一个用来表示表达式的数据类型的字符串。


    typeof[()expression[]] ;

    expression 参数是需要查找类型信息的任意表达式


    说明

    typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."

    typeof 语法中的圆括号是可选项。 

  • 相关阅读:
    [JZOJ 5788] 餐馆
    [JZOJ 5778] 没有硝烟的战争
    problems_scala
    好迷茫,好迷茫啊
    公布下我的数据库操作层
    关于数据库大并发量(未完成)
    关于http协议头
    管理心得体会
    数据库表分区
    公共的Json操作类
  • 原文地址:https://www.cnblogs.com/wumingcong/p/4650333.html
Copyright © 2011-2022 走看看