1.判定数据类型

作者: 非勤能补拙 | 来源:发表于2017-12-26 16:40 被阅读0次

js数据类型: 字符串 数字 布尔 数组 对象 null undefined

出现undefined的情况:

1.变量声明后未赋初始值  var a

2.函数没有返回值            function(){} a=k()

3.对象没有赋值的属性,该属性的值为undefined

var o= new Object(); o.p

4.调用函数时,应该提供的参数没有提供,该参数等于undefined

function f(x){console.log(x)} f()

出现null的情况

1.作为函数的参数,表示该函数的参数不是对象。

2.作为对象原型链的终点

undefined和null在if语句中,都会被自动转为false,相等运算符甚至报告两者相等。


使用typeof运算符返回一个用来表示表达式的数据类型的字符串

可能的字符串有:number string Boolean object functionundefined

常用返回值说明

表达式                           返回值

typeof undefined           undefined

typeof null                     object

typeof ture                    boolean

typeof 123                    number  

typeof 'abc'                   string

typeof function(){}         function

typeof {}                        object

typeof []                        object

typeof unknownVariable uncefined

类型返回值都是字符串,而且都是小写打头


instanceof运算符 在引用类型值判断类型的时候,typeof运算符会出现一个问题,无论引用的是换一个什么类型的对象,它都返回object。

instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上。

obj instanceof Object //true 实例obj在不在Object构造函数中

1.instanceof的普通用法,obj instanceof Object 检测Object.prototype是否存在于参数obj的原型链上。

Person的原型在p的原型链上

function Person(){};

var p=new Person();

console.log(p instanceof Person);//true

2.继承中判断实例是否属于它的父类

function Person(){};

function Student(){};

var p=new Person();

Student.prototype=p;//继承原型

var s=new Student();

console.log(s instanceof Student);//true

console.log(s instanceof Person);//true

3.复杂用法

未应用

constructor只能对已有变量进行判断,而typeof则可对未声明变量进行判断(返回undefined)

var test=new Array();

if (test.constructor==Array)

{

document.write("This is an Array");

}

if (test.constructor==Boolean)

{

document.write("This is a Boolean");

}

if (test.constructor==Date)

{

document.write("This is a Date");

}

if (test.constructor==String)

{

document.write("This is a String");

}


4.最完美的判定方法

Object.prototype.toString.call(obj)检测对象类型

不能直接使用obj.toString()。

toString为Object的原型方法,而Array function等类型作为Object的实例,都重写了toString方法。

不同的对象类型调用toString方法,根据原型链的姿势,调用的是对应的重写之后的toString方法,function类型返回内容为函数体的字符串,Array类型返回元素组成的字符串。而不会去调用Object上原型toString方法,返回对象的具体类型。所以采用obj.toString()不能得到其对象类型。只能将obj转换成字符串类型。

vararr=[1,2,3];

console.log(Array.prototype.hasOwnProperty("toString"));//true

console.log(arr.toString());//1,2,3deleteArray.prototype.toString;//delete操作符可以删除实例属性

console.log(Array.prototype.hasOwnProperty("toString"));//false

console.log(arr.toString());//"[object Array]"

删除了Array的toString方法后,同样再采用arr.toString()方法调用时,不再有屏蔽Object原型方法的实例方法,因此沿着原型链,arr最后调用了Object的toString方法,返回了和Object.prototype.toString.call(arr)相同的结果。

相关文章

  • 1.判定数据类型

    js数据类型: 字符串 数字 布尔 数组 对象 null undefined 出现undefined的情况: 1....

  • 数据类型的判定

    数据类型的判定 js六大数据类型:number、string、object、Boolean、null、undefi...

  • Python内置数据类型

    类型判定 type(a) : 返回a的数据类型,注意是数据类型不是字符串内置函数 isinstance(obj, ...

  • JavaScript 数据类型与变量提升

    1.JS 中有哪些数据类型?JavaScript提供了一种判定给定变量数据类型的手段——typeof操作符,对值或...

  • 素数筛法——1. 素数判定

    素数判定问题 题目描述 给定一个数n,要求判断其是否为素数(0,1,负数都是非素数)。 输入描述: 测试数据有多组...

  • 1. 数据类型

    修饰符 数据类型 字符串(String) 哈希表(Object) 数组(Array) 数字(Number) 布尔(...

  • 1. 数据类型

    基本概念 在PHP中定义变量是不支持指定类型的,而是在其赋值后根据内容自动处理。比如一个变量,给其赋值一个字符串,...

  • 1.数据类型

    undefined、null null是一个表示“空”的对象,转为数值时为0;undefined是一个表示"此处无...

  • Haskell笔记2

    1 Polymorphic Types 在很多情况下,不同的数据类型会有相同功能的函数,例如判定相等或者不等,Li...

  • 每天读点书之“把时间当作朋友——现实”

    知识可以分为判定类和概念类知识。其关系是判定类知识用于鉴定概念类知识的有效性。 本章讲了五点判定类知识: 1.速成...

网友评论

    本文标题:1.判定数据类型

    本文链接:https://www.haomeiwen.com/subject/bhnhqxtx.html