关于js中数据类型的自我理解

来源:互联网 发布:php 下载pdf文件 编辑:程序博客网 时间:2024/05/18 01:41

逐渐更新


本人也是初步学习js,学到js的数据类型的时候  发现书上和网上的资源对这讲的并不是很清楚,自己有些地方有些疑惑,所以我写出来常常提醒自己。


以下均为我自己的理解  可能说法不对  如若有不对的地方非常欢迎大家的指正


在 JavaScript 中有 5 种不同的数据类型:

  • string
  • number
  • boolean
  • object
  • function

3 种对象类型:

  • Object
  • Date
  • Array

2 个不包含任何值的数据类型:

  • null
  • undefined

我们可以用 typeof运算符来确定基本类型:string,number,boolean,object,undefined,function.(只会有这6种返回值)

如果typeof运算符返回object我们再使用instanceof来确定该对象是否属于某个具体类型

注意:

typeof null //返回object

typeof undefined //返回undefined


用var instance Type 来确定变量是否是某一种对象类型

var o=[];  alert(o instanceof Array);//true  alert(o instanceof Object);//true  var f=function(){}  alert(f instanceof Function);//true  alert(f instanceof Object);//true  

如果要判断一个对象是否为某个具体类(子类)的实例,可以看该对象的constructor属性。

var d=new Date();  alert(d instanceof Object);//true  alert(d.constructor==Object);//false  alert(d.constructor==Date);//true  




0 0
原创粉丝点击