本文章来介绍在js使用中的一个函数typeof用法, typeof 运算符把类型信息当作字符串返回,包括有大家常有变量类型。

typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."我们可以使用typeof来获取一个变量是否存在,如if(typeof a!="undefined"){},而不要去使用if(a)因为如果a不存在(未声明)则会出错,对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。

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

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”

对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。

如果我们希望获取一个对象是否是数组,或判断某个变量是否是某个对象的实例则要选择使用instanceof。instanceof用于判断一个变量是否某个对象的实例,如var a=new Array();alert(a instanceof Array);会返回true,同时alert(a instanceof Object)也会返回true;这是因为Array是object的子类。再如:function test(){};var a=new test();alert(a instanceof test)会返回true。

友情提示

a instanceof Object 得到true并不是因为 Array是Object的子对象,而是因为 Array的prototype属性构造于Object,Array的父级是Function

Examples

// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number(1) === 'number'; // but never use this form!// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof always returns a string
typeof String("abc") === 'string'; // but never use this form!// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // but never use this form!// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined'; // Objects
typeof {a:1} === 'object';// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === 'object';typeof new Date() === 'object';// The following is confusing. Don't use!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';// Functions
typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';

null

// This stands since the beginning of JavaScript
typeof null === 'object';

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof return value. (reference)

A fix was proposed for ECMAScript (via an opt-in), but wasrejected. It would have resulted intypeof null === 'null'.

Regular expressions

Callable regular expressions were a non-standard addition in some browsers.

typeof /s/ === 'function'; // Chrome 1-12 Non-conform to ECMAScript 5.1
typeof /s/ === 'object';   // Firefox 5+  Conform to ECMAScript 5.1

Exceptions

All current browsers expose a non-standard host object document.all with type Undefined.

typeof document.all === 'undefined';

Although the specification allows custom type tags for non-standard exotic objects, it requires those type tags to be different from the predefined ones. The case of document.all having type tag'undefined' must be classified as an exceptional violation of the rules.

js中typeof用法详细介绍相关推荐

  1. php中sisson用法,详细介绍php中session的用法

    PHP中的session默认情况下是使用客户端的Cookie.当客户端的Cookie被禁用时,会自动通过Query_String来传递. Php处理会话的函数一共有11个,我们详细介绍一下将要用到几个 ...

  2. JS中typeof的用法

    转载自  JS中typeof的用法 js是一门弱语言,它在声明变量时无需确定变量的类型,js在运行时会自动判断.那么如何判断一个变量的类型呢,js提供了typeof运算符,用来检测一个变量的类型. 1 ...

  3. python中soup_python中BeautifulSoup的详细介绍(附代码)

    本篇文章给大家带来的内容是关于python中BeautifulSoup的详细介绍(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. Beautiful Soup提供一些简单的. ...

  4. Android manifest文件中的标签详细介绍

    Android manifest文件中的标签详细介绍 概要 每一个Android应用都应该包含一个manifest文件,即AndroidManifest.xml.它包含了程序运行的一些必备信息,比如: ...

  5. 【MADDPG(MPE)——环境配置与用法详细介绍(多智能体强化学习))】

    MADDPG(MPE)--环境配置与用法详细介绍(多智能体强化学习) MADDPG(MPE) 介绍 MPE环境安装教程 前期准备 MPE 安装包介绍 MPE 安装环境要求 开始安装 环境测试 MPE环 ...

  6. Cesium中图元Primitive详细介绍及案例

    Cesium从入门到项目实战总目录: 点击 文章目录 Cesium中图元Primitive详细介绍 Cesium中Primitive案例 Cesium中图元Primitive详细介绍 在Cesium中 ...

  7. pythonexcel介绍_Python 中pandas.read_excel详细介绍

    Python 中pandas.read_excel详细介绍 #coding:utf-8 import pandas as pd import numpy as np filefullpath = r& ...

  8. 2020-12-09 深度学习 卷积神经网络中感受野的详细介绍

    卷积神经网络中感受野的详细介绍 1. 感受野的概念 在卷积神经网络中,感受野(Receptive Field)的定义是卷积神经网络每一层输出的特征图(feature map)上的像素点在输入图片上映射 ...

  9. php return直接输出,PHP中return用法详细解读

    原标题:PHP中return用法详细解读 在大部分编程语言中,return关键字可以将函数的执行结果返回,PHP中return的用法也大同小异,对初学者来说,掌握PHP中return的用法也是学习PH ...

最新文章

  1. 在DataGrid中显示图片
  2. 字段变成小写 序列化_序列化/反序列化
  3. Unity3D为何能跨平台?聊聊CIL(MSIL)
  4. 四则运算(可怜的二柱子)2
  5. mySQL微信小程序的div_做一个微信小程序的完整流程
  6. 序列化包含多种不明类型的集合
  7. angular 字符串转换成数字_Python成为专业人士笔记–String字符串方法
  8. 霍金遗作《十问:霍金沉思录》出版 马化腾作跋纪念
  9. ip代理服务器软件25探索云速捷_使用代理进行Web网页抓取的基础
  10. Linux——RHCE试题与答案详解
  11. Unity3D导出的EXE不用显示分辨率选择界面
  12. Vue中使用Video标签播放 <解析后的短视频>去水印视频无响应
  13. ElasticJob
  14. java.lang.NoSuchMethodError: com.xxx.xxx.xxx.po.xxxPo.setXXX(D)V
  15. VIF,共线相关性理解
  16. php怎么插入图层,PS制作-把图片添加到图层的4种方法
  17. CANopen协议基础知识
  18. jquery 鼠标拖动排序Li或Table
  19. S02_CH15_ AXI_OLED 实验
  20. lol1月8日服务器维护,LOL1月16日更新维护公告 LOL8.1新版本更新到几点

热门文章

  1. 盛大 牛人 blog
  2. Silverlight学习之——如何在 Silverlight 中使用 Deep Zoom
  3. Javascript控制Radio HTML控件
  4. kugoo应用心得——p2p下载,共享文件
  5. Django开发环境准备
  6. asp.net core上使用redis探索(1)
  7. JavaScript值得注意的小知识点
  8. zabbix3.2监控centos6网卡流量
  9. 在通知栏上玩游戏,Steve iOS 游戏实现思路
  10. Android开发学习——android体系结构