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

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

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

数据类型深入理解

数据类型分类

基本(值)类型(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) // 根据类型创建的实例对象

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

查看所有标签

猜你喜欢:

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

软件测试

软件测试

乔根森 / 韩柯 / 机械工业出版社 / 2003-12-1 / 35.00元

《软件测试》(原书第2版)全面地介绍了软件测试的基础知识和方法。通过问题、图表和案例研究,对软件测试数学问题和技术进行了深入的研究,并在例子中以更加通用的伪代码取代了过时的Pascal代码,从而使内容独立于具体的程序设计语言。《软件测试》(原书第2版)还介绍了面向对象测试的内容,并完善了GUI测试内容。一起来看看 《软件测试》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码