判断JS变量类型

function getType(o) {
  var _t;
  return ((_t = typeof(o)) == "object" ? Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
}
时间: 2024-08-28 06:42:11

判断JS变量类型的相关文章

js变量类型及检查

一.变量的类型 JavaScript 有六种数据类型.主要的类型有 Number.String.object 以及 Boolean 类型,其他两种类型为 null 和 undefined.var obj = {x:[1,2],y:23};//Object类型 i=100;//Number类型 i="variable";//String类型 i={x:4};//Object类型 i=[1,2,3];//Array类型var men = true; // Boolean 类型.变量命名:J

JS—变量类型

<html> <head> <title>03-JS变量类型</title> <!-- 设置编码 --> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <script type="text/javascript"> /*java中的变量分类: 基本数据类型 引用数据类型 js中

初窥Python(四)——三种方法判断python变量类型

python 是动态语言,定义变量时不用指定变量类型,在代码执行过程中,会根据变量的值确定变量类型.python 中常用的变量类型有 int  float  long  bool  str  list  tuple set dict 等,常用的变量类型的有 types 库及内置的 type(object) 和 isinstance(object,class-or-type-or-tuple) 方法,下面分别来看一下如何通过这些方法进行变量类型的判断. 1.使用types库结合type(objec

javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)

function isArray(o) { return Object.prototype.toString.apply(o) === “[object Array]”;}function isFunction(o) { return Object.prototype.toString.apply(o) === ”[object Function]“;} --JQuery  源码有....

js中typeof可以准确判断哪些变量类型

typeof 运算符返回一个用来表示表达式的数据类型的字符串.  可能的字符串有:"number"."string"."boolean"."object"."function" 和 "undefined". 常用返回值说明 表达式 返回值 typeof undefined 'undefined' typeof null 'object' typeof true 'boolean' typ

js 变量类型、相互转换

1.string    var string = "this is mine"    string.substring();            //截取函数    string.toLowerCase();        //换换小写    string.toUpperCase();        //转换大写    string.indexOf("is") > -1;    //字符串检索,检索不到返回-1    string.replace("

判断一个变量类型是数组还是对象

因为无论是数组还是对象,对于typeof的操作返回值都为object,所以就有了区分数组类型和对象类型的需要: 方一:通过length属性:一般情况下对象没有length属性值,其值为undefiend,而数组的length值为number类型 缺点:非常不实用,当对象的属性存在length,且其值为number(比如类数组),则该方法失效,不建议使用,看看即可. *方二:通过instanceof来判断区分 var arr = [1, 2, 3]; var obj = { name: 'lyl'

js变量类型

js中有null和undefined,null是指对象不存在,undefined是指原生数据不存在 var h = {name:'lisi',age:28};console.log(h.name)//对象用的是点语法,php中是name->'lisi' 下面是数组,数组用的是[]语法 1 var arr = ['a',3,'hello',true]; 2 console.log(arr); 3 //显示结果为 4 ["a", 3, "hello", true]

如何去判断一个变量类型是数组还是对象

因为无论是对象还是数组,对于typeof操作返回的都是object,那么怎么去区分他们呢? 1.通过length属性:一般情况下对象没有length属性值,其值为undefiend,而数组的length值为number类型 缺点:非常不实用,当对象的属性存在length,且其值为number(比如类数组),则该方法失效,不建议使用,看看即可. 2.通过instanceof来判断区分 var arr = [1, 2, 3]; var obj = { name: 'lyl', age: 18, 1: