JavaScript-ECMAScript

undefined

原始值的初始值,未定义

存在undefined的情况有:

  • 已声明未赋值的变量
  • 访问对象不存在的属性
  • 函数没有返回值的情况
  • 参数没有传入,实参没有传入获取形参
  • void()
  1. console.log(Boolean(undefined)); //false
  2. console.log(String(undefined)); //undefined
  3. console.log(Number(undefined)); //NaN

null

对象的初始值,空对象的引用

  1. console.log(typeof null); //object
  1. console.log(Boolean(null)); //false
  2. console.log(String(null)); //null
  3. console.log(Number(null)); //0

undefinednull

  1. var a = undefined > 0;
  2. console.log(a); //false
  3. var a = undefined < 0;
  4. console.log(a); //false
  5. var a = undefined == 0;
  6. console.log(a); //false
  7. var a = null > 0;
  8. console.log(a); //false
  9. var a = null < 0;
  10. console.log(a); //false
  11. var a = null == 0;
  12. console.log(a); //false
  13. var a = undefined == null;
  14. console.log(a); //true
  15. //undefined和null并不是一个数据类型
  16. var a = undefined === null;
  17. console.log(a); //false
  1. console.log(undefined == null); //true
  2. console.log(undefined === null); //false

undefinednull是不可以设置任何的属性和方法

  1. console.log(undefined.length); //报错
  2. console.log(null.length); //报错

NaN

  1. console.log(0 / 0); //NaN -> Not a Number 数字类型
  2. console.log(1 / 0); //Infinity 正无穷 数字类型
  3. console.log(-1 / 0); //-Infinity 负无穷 数字类型

NaN与包括自己在内任何东西都不相等

  1. //null => 0 不是非数字
  2. console.log(isNaN(null)); //false

包装类

  1. var a = 'abc';
  2. //为什么能够打印出属性值?
  3. //1. 字符串转对象类型 new String('abc')
  4. //2. console.log(new String('abc').length);
  5. //3. 销毁实例 new String('abc') = null;
  6. console.log(a.length); //3
  1. console.log('abc' == String('abc')); //true
  2. console.log('abc' === String('abc')); //true
  3. console.log(new String('abc') == String('abc')); //true
  4. //new String('abc')为对象
  5. //String('abc'))为字符串
  6. console.log(new String('abc') === String('abc')); //false

++&--

  • a++:先打印后运算
  • ++a:先运算赋值再打印

typeof()

JS的typeof()可能返回的值有:object/boolean/number/string/undefined/function

  1. console.log(typeof ('abc')); //string
  2. console.log(typeof (1)); //number
  3. console.log(typeof (true)); //boolean
  4. console.log(typeof (undefined)); //undefined
  5. console.log(typeof (null)); //object
  6. console.log(typeof ({})); //object
  7. console.log(typeof ([])); //object
  8. console.log(typeof (function () {})); //function

Number()&parseInt()

parseInt()不管布尔值是否先转换为1,而是直接整形为NaN

  1. console.log(Number(true)); //1
  2. console.log(Number(false)); //0
  3. console.log(Number(null)); //0
  4. console.log(Number(undefined)); //NaN
  5. // parseInt()不管布尔值是否先转换为1,而是直接整形为NaN
  6. console.log(parseInt(true)); //NaN
  7. console.log(parseInt(false)); //NaN
  8. console.log(parseInt(null)); //NaN
  9. console.log(parseInt(undefined)); //NaN

Boolean()

  1. console.log(Boolean(1)); //true
  2. console.log(Boolean(null)); //false
  3. console.log(Boolean(false)); //false
  4. console.log(Boolean(undefined)); //false
  5. console.log(Boolean(NaN)); //false
  6. console.log(Boolean(0)); //false
  7. console.log(Boolean('')); //false

隐式转换

  • 加号会将字符串转为数字
  • 字符串加数字转为字符串
  • 乘除余减会将字符串转数字
  • 比较运算符将字符串转数字

内置对象

JS的原生标准内置对象:

  • 原始值:undefinednullnumberstringboolean
  • 引用值:objectarray,function
  • 包装对象:NumberStringBoolean
  • 构造函数:ObjectFunctionDateErrorRegExpArray
  • 内置对象:MathJSONArguments

宿主对象

宿主环境提供给我们的扩展对象,接口:

windowdocumenthistorynavigator

WebsAPI:POSTmessageindexDB,XML,ActiveXObject

数组方法

扁平化/转字符串:

  • Array.prototype.toString.call():深度扁平化
  • Array.prototype.join():只能扁平一层
  • String.prototype.split():逆操作

堆栈:

  • Array.prototype.push():最后一位新增,返回值当前修改后数组长度
  • Array.prototype.pop():第一位删除,返回值当前修改后数组长度
  • Array.prototype.unshift()第一位增加,,返回值当前修改后数组长度
  • Array.prototype.shift()最后一位删除,返回值当前修改后数组长度

排序:

  • Array.prototype.reverse():返回值倒序之后原数组
  • Array.prototype.sort():返回值排序之后原数组

拼接:

  • Array.prototype.concat()返回新数组,不能多维拼接

删改:

  • Array.prototype.slice():返回新数组,可以将字符串转为数组,也可以将类数组转为数组
  • Array.prototype.splice():会修改原数组,参数一位(返回左闭右开的删除元素返回的数组)

查找:

  • Array.prototype.indexOf():返回索引从左往右
  • Array.prototype.lastIndexOf():返回索引从右往左
  • Array.prototype.find():返回第一个复合条件的成员
  • Array.prototype.findIndex():返回第一个符合条件的成员的索引

填充/创建数组:

  • Array.of()
  • Array.from()
  • Array.prototype.keys()
  • Array.prototype.fill()

包含:

  • Array.prototype.includes()

遍历:

forEach, map, reduce, reduceRight, some, every, filter, Array.keys(), Array.values(), Array.entries()