js中typeof用法详细介绍

本文章来介绍在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


  1. // Numbers
  2. typeof 37 === ‘number‘;
  3. typeof 3.14 === ‘number‘;
  4. typeof(42) === ‘number‘;
  5. typeof Math.LN2 === ‘number‘;
  6. typeof Infinity === ‘number‘;
  7. typeof NaN === ‘number‘; // Despite being "Not-A-Number"
  8. typeof Number(1) === ‘number‘; // but never use this form!
  9. // Strings
  10. typeof "" === ‘string‘;
  11. typeof "bla" === ‘string‘;
  12. typeof (typeof 1) === ‘string‘; // typeof always returns a string
  13. typeof String("abc") === ‘string‘; // but never use this form!
  14. // Booleans
  15. typeof true === ‘boolean‘;
  16. typeof false === ‘boolean‘;
  17. typeof Boolean(true) === ‘boolean‘; // but never use this form!
  18. // Symbols
  19. typeof Symbol() === ‘symbol‘
  20. typeof Symbol(‘foo‘) === ‘symbol‘
  21. typeof Symbol.iterator === ‘symbol‘
  22. // Undefined
  23. typeof undefined === ‘undefined‘;
  24. typeof declaredButUndefinedVariable === ‘undefined‘;
  25. typeof undeclaredVariable === ‘undefined‘;
  26. // Objects
  27. typeof {a:1} === ‘object‘;
  28. // use Array.isArray or Object.prototype.toString.call
  29. // to differentiate regular objects from arrays
  30. typeof [1, 2, 4] === ‘object‘;
  31. typeof new Date() === ‘object‘;
  32. // The following is confusing. Don‘t use!
  33. typeof new Boolean(true) === ‘object‘;
  34. typeof new Number(1) === ‘object‘;
  35. typeof new String("abc") === ‘object‘;
  36. // Functions
  37. typeof function(){} === ‘function‘;
  38. typeof class C {} === ‘function‘;
  39. typeof Math.sin === ‘function‘;

null


  1. // This stands since the beginning of JavaScript
  2. 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 was
rejected
. It would have resulted intypeof null === ‘null‘.

Regular expressions

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


  1. typeof /s/ === ‘function‘; // Chrome 1-12 Non-conform to ECMAScript 5.1
  2. 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.

原文地址:https://www.cnblogs.com/ysk123/p/9818723.html

时间: 2024-08-26 23:44:13

js中typeof用法详细介绍的相关文章

js中typeOf用法

JS中的变量是松散类型(即弱类型)的,可以用来保存任何类型的数据. typeof 可以用来检测给定变量的数据类型,可能的返回值:1. 'undefined' --- 这个值未定义: 2. 'boolean'    --- 这个值是布尔值: 3. 'string'        --- 这个值是字符串: 4. 'number'     --- 这个值是数值: 5. 'object'       --- 这个值是对象或null: 6. 'function'    --- 这个值是函数. Js代码  

详解js中typeof、instanceof与constructor

详解js中typeof.instanceof与constructor typeof返回一个表达式的数据类型的字符串,返回结果为js基本的数据类型,包括number,boolean,string,object,undefined,function.语法为typeof(data) 或 typeof data instanceof则为判断一个对象是否为某一数据类型,或一个变量是否为一个对象的实例;返回boolean类型 语法为 o instanceof A 以下为综合实例: 1<script type

MySQL replace into 用法详细介绍

MySQL replace into 用法(insert into 的增强版) 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果存在,则更新. 在 SQL Server 中可以这样处理: if not exists (select 1 from t where id = 1) insert into t(id, update_time) values(1, getdate()) else update t set update_time

js中prototype用法(转)

JavaScript能够实现的面向对象的特征有:·公有属性(public field)·公有方法(public Method)·私有属性(private field)·私有方法(private field)·方法重载(method overload)·构造函数(constructor)·事件(event)·单一继承(single inherit)·子类重写父类的属性或方法(override)·静态属性或方法(static member) 例子一(JavaScript中允许添加行为的类型):可以在

js中typeof和instanceof用法区别

typeof和instanceof的区别 typeof和instanceof都可以用来判断变量,它们的用法有很大区别: typeof会返回一个变量的基本类型,只有以下几种:number,boolean,string,object,undefined,function:例: alert(typeof(1));//number alert(typeof("abc"));//string alert(typeof(true));//boolean alert(typeof(m));//und

js中推断对象详细类型

大家可能知道js中推断对象类型能够用typeof来推断. 看以下的情况 <script> alert(typeof 1);//number alert(typeof "2");//string alert(typeof [1,2,3]);//object alert(typeof {"name":"zhuhui"})//object </script> 从上面中我们能够看出数组和普通对象用typeof推断出来都是objec

Jquery中AJAX参数详细介绍

在使用jquery的时候,我们经常用到jquery中对ajax的封装,下面对ajax函数的各参数详细说明和讲解,以便更好的理解和使用 $.get(url, data, callback,type) 和 $.post(url, data, callback, type). 1. jQuery.ajax( options ) : 通过 HTTP 请求加载远程数据 这个是jQuery 的底层 AJAX 实现.简单易用的高层实现见 $.get, $.post 等. $.ajax() 返回其创建的 XML

JS中 typeof 与 instanceof 的区别

JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的:typeof是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型.typeof 一般只能返回如下几个结果:number,boolean,string,function,object,undefined.(Array,Null 均返回 object ,无法判断这些类型)例子: typeof(1): number

JS中typeof与instanceof的区别 (2010-05-26 10:47:40

JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: typeof typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型. 它返回值是一个字符串,该字符串说明运算数的类型.typeof 一般只能返回如下几个结果: number,boolean,string,function,object,undefined.我们可以使用 typeof 来获取一个变量是否存在,如 if(typeof a!="un