赞
踩
ECMAScript(es):负责翻译,为js的核心,解释器。
5种基本数据类型:Undefined、Null、Boolean、Number、String,Symbol(es6新增)
1种复杂类型:object
5种引用类型:Array,Object,Function,Date,RegExp
3种基本包装类型:Boolean,Number,String
2种单体内置对象:Global,Math
- const a = ""
- console.log(typeof(a)) => String
-
- const b = 1
- console.log(typeof(b)) => Number
-
- const c
- console.log(typeof(c)) =>Undefined
-
- const d = []
- console.log(typeof(d)) => Object
-
- const e = {}
- console.log(typeof(e)) =>Object
-
- const f = null
- console.log(typeof(f)) =>Object //null 作为尚未创建的对象
-
-
-
-
- const arr = []
- console.log(arr instanceof Array)=> true
-
- console.log(null instanceof Object) ---> false
-
- console.log([function] instanceof Object | Function) --> true
这种方式可以将全部的数据类型检测出来 推荐使用。因为toString是Object的原型方法, 而 Array Function 等都是Object的实例。都重写了toString 方法。返回的是类型的字符串
- Object.prototype.toString.call(null) => [object Null]
-
- Object.prototype.toString.call(Math) => [object Math]
-
- Object.prototype.toString.call(function(){}) => [object Function]
-
- Objdec.prototype.toString.call(new Date) => [object Date]
-
- Object.prototype.toString.call(Symbol()) => [object Symbol]
-
- Object.prototupe.toString.call(undefined) => [object Undefined]
-
- Object.prototype.toString.call(123) => [object Number]
-
- Object.prototype.toString.call(true) => [object Boolean]
-
- Object.prototype.toString.call('123') => [object String]
-
- Object.prototype.toString.call({}) => [object Object]
-
- Object.prototype.toString.call([]) => [object Array]
判断对象的构造函数。
1. null 是js 原型链的起点,没有构造函数
2. undefined 没有构造函数
3. [].constructor === Array ---> true
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。