【1】JavaScript 基础深入——数据类型深入理解与总结
栏目: JavaScript · 发布时间: 6年前
内容简介:注意:首先要理解注意:
数据类型深入理解
数据类型分类
基本(值)类型(5种)
-
String:任意字符串 -
Number:任意的数字 -
boolean:true/false -
null:null -
undefined:undefined
对象(引用)类型(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类型
===
-
可以判断
undefined和null
ccc === "undefined" // true null === null // true
总结
-
typeof:-
可以判断
undefined/ 数值 / 字符串 / 布尔值 /function -
不能判断
null与object,array与objecttypeof null // "object" typeof [] // "object"
-
可以判断
-
instanceof:A instanceof B
-
===:-
可以判断
undefined,null
-
可以判断
undefined
与 null
的区别?
-
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) // 根据类型创建的实例对象
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。