【1】JavaScript 基础深入——数据类型深入理解与总结

栏目: JavaScript · 发布时间: 6年前

内容简介:注意:首先要理解注意:

数据类型深入理解

数据类型分类

基本(值)类型(5种)

  • String :任意字符串
  • Number :任意的数字
  • booleantrue / false
  • nullnull
  • undefinedundefined

对象(引用)类型(3种)

Object
Array
Function

数据类型判断(3种方式)

typeof :返回数据类型的字符串表达

var a
console.log(a) // undefined
console.log(typeof a) // "undefined"
console.log(a === undefined) // true
console.log(typeof a === undefined) // false
console.log(typeof a === "undefined") // true
console.log(undefined === "undefined") // false

a = 4
console.log(typeof a) // "number"
console.log(typeof a === Number) // false
console.log(typeof a === "number") // true

a = "hahha"
console.log(typeof a) // "string"

a = false
console.log(typeof a) // "boolean"

a = null
console.log(typeof a) // object
console.log(a === null) // true

注意: typeof 返回的是数据类型的 字符串 表达形式。

typeof true //"boolean"
typeof "hahha" //"string"
typeof 12 //"number"
typeof null //"object"
typeof ccc //"undefined"

typeof function(){} //"function"
typeof {} //"object"

instanceof :类型的实例

首先要理解 instanceof 的含义:

  • instance例子 的意思, A instanceof B 实际上是判断 A 是否是 B 的一个 实例 。理解了这一点,就不难判断类型了。
var b1 = {
    b2: [1, "hehe", console.log],
    b3: function () {
        console.log("b3")
        return function  () {
            return "Mandy"
        }
    }
}

console.log(b1 instanceof Object) // true
console.log(b1.b2 instanceof Array, b1.b2 instanceof Object) // true true
console.log(b1.b3 instanceof Function, b1.b3 instanceof Object) //true true

console.log(typeof b1.b2) // "object"
console.log(typeof b1.b3) // "function"
console.log(typeof b1.b2[1]) // "string"
console.log(typeof b1.b2[2]) // "function"
b1.b2[2](555) // 555
console.log(b1.b3()()) // "b3" "Mandy"

注意:

  • 函数既是 Function 类型,也是 Object 类型
  • 数组既是 Array 类型,也是 Object 类型

===

  • 可以判断 undefinednull
ccc === "undefined" // true
null === null // true

总结

  • typeof :

    • 可以判断 undefined / 数值 / 字符串 / 布尔值 / function
    • 不能判断 nullobject , arrayobject

      typeof null // "object"
      
      typeof [] // "object"
  • instanceof :

    A instanceof B
    
  • === :

    • 可以判断 undefined , null

undefinednull 的区别?

  • undefined 代表定义了,未赋值
  • null 代表定义了,并且赋值了,只是赋的值为 null
// undefined与null的区别?
var a
console.log(a)  // undefined
a = null
console.log(a) // null

什么时候给变量赋值为 null ?

typeof null === "Object" 
//起始
var b = null  // 初始赋值为null, 表明将要赋值为对象
//确定对象就赋值
b = ['atguigu', 12]
//最后
b = null // 让b指向的对象成为垃圾对象(被垃圾回收器回收)

严格区别 变量类型数据类型

  • 数据的类型:

    • 基本类型
    • 对象类型
  • 变量的类型(变量内存值的类型)

    • 基本(值)类型:保存的就是基本类型的数据
    • 引用类型:保存的是地址值

理解 实例类型

// 实例: 实例对象
// 类型: 类型对象
function Person (name, age) {// 构造函数  类型
  this.name = name
  this.age = age
}
var p = new Person('tom', 12) // 根据类型创建的实例对象

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ext JS学习指南

Ext JS学习指南

(美)布莱兹、(美)拉姆齐、(美)弗雷德里克 / 孔纯、肖景海、张祖良 / 人民邮电出版社 / 2009-10 / 39.00元

《Ext JS学习指南》系统化地介绍了Ext JS的基础知识,从框架的下载安装到各种常用小部件的实例介绍,从如何自定义小部件到Ext JS代码复用和扩展机制,《Ext JS学习指南》覆盖了Ext JS知识的所有主要方面。作为Web 2.0时代企业应用的一把开发利器,Ext JS为企业应用开发的表现层实现提供了优秀的解决方案。 如果你掌握了HTML,并且了解一般的CSS和JavaScript的......一起来看看 《Ext JS学习指南》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具