发表于: 2020-06-25 19:52:08

1 2183


今天完成的事情:

1.回顾任务


明天计划的事情:

1.回顾任务


遇到的问题和收获:

判断数据类型

1.typeof

typeof 用来判断各种数据类型,有两种写法:typeof xxx , typeof(xxx)

例如:

typeof 2 number
typeof null object
typeof {} object
typeof [] object
typeof (function(){}) function
typeof undefined undefined
typeof '222' string
typeof true boolean


2. instanceof

判断已知对象类型的方法.instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

  var c= [1,2,3];
var d = new Date();
var e = function(){alert(111);};
var f = function(){this.name="22";};
console.log(c instanceof Array) //true
console.log(d instanceof Date) //true
console.log(e instanceof Function) //true
// console.log(f instanceof function ) //false


3.constructor

根据对象的constructor判断,返回对创建此对象的数组函数的引用。

    var c= [1,2,3];
               var d = new Date();
               var e = function(){alert(111);};
               alert(c.constructor === Array) ----------> true
               alert(d.constructor === Date) -----------> true
               alert(e.constructor === Function) -------> true
//注意: constructor 类继时会


4.prototype

所有数据类型均可判断:Object.prototype.toString.call

这是对象的一个原生原型扩展函数,用来更精确的区分数据类型。

var gettype=Object.prototype.toString
gettype.call('aaaa') [object String]
gettype.call(2222) [object Number]
gettype.call(true) [object Boolean]
gettype.call(undefined) [object Undefined]
gettype.call(null) [object Null]
gettype.call({}) [object Object]
gettype.call([]) [object Array]
gettype.call(function(){}) [object Function]






返回列表 返回列表
评论

    分享到