JavaScript-ECMAScript
undefined
原始值的初始值,未定义
存在undefined的情况有:
- 已声明未赋值的变量
- 访问对象不存在的属性
- 函数没有返回值的情况
- 参数没有传入,实参没有传入获取形参
void()
console.log(Boolean(undefined)); //falseconsole.log(String(undefined)); //undefinedconsole.log(Number(undefined)); //NaN
null
对象的初始值,空对象的引用
console.log(typeof null); //object
console.log(Boolean(null)); //falseconsole.log(String(null)); //nullconsole.log(Number(null)); //0
undefined和null
var a = undefined > 0;console.log(a); //falsevar a = undefined < 0;console.log(a); //falsevar a = undefined == 0;console.log(a); //falsevar a = null > 0;console.log(a); //falsevar a = null < 0;console.log(a); //falsevar a = null == 0;console.log(a); //falsevar a = undefined == null;console.log(a); //true//undefined和null并不是一个数据类型var a = undefined === null;console.log(a); //false
console.log(undefined == null); //trueconsole.log(undefined === null); //false
undefined和null是不可以设置任何的属性和方法
console.log(undefined.length); //报错console.log(null.length); //报错
NaN
console.log(0 / 0); //NaN -> Not a Number 数字类型console.log(1 / 0); //Infinity 正无穷 数字类型console.log(-1 / 0); //-Infinity 负无穷 数字类型
NaN与包括自己在内任何东西都不相等
//null => 0 不是非数字console.log(isNaN(null)); //false
包装类
var a = 'abc';//为什么能够打印出属性值?//1. 字符串转对象类型 new String('abc')//2. console.log(new String('abc').length);//3. 销毁实例 new String('abc') = null;console.log(a.length); //3
console.log('abc' == String('abc')); //trueconsole.log('abc' === String('abc')); //trueconsole.log(new String('abc') == String('abc')); //true//new String('abc')为对象//String('abc'))为字符串console.log(new String('abc') === String('abc')); //false
++&--
a++:先打印后运算++a:先运算赋值再打印
typeof()
JS的typeof()可能返回的值有:object/boolean/number/string/undefined/function
console.log(typeof ('abc')); //stringconsole.log(typeof (1)); //numberconsole.log(typeof (true)); //booleanconsole.log(typeof (undefined)); //undefinedconsole.log(typeof (null)); //objectconsole.log(typeof ({})); //objectconsole.log(typeof ([])); //objectconsole.log(typeof (function () {})); //function
Number()&parseInt()
parseInt()不管布尔值是否先转换为1,而是直接整形为NaN
console.log(Number(true)); //1console.log(Number(false)); //0console.log(Number(null)); //0console.log(Number(undefined)); //NaN// parseInt()不管布尔值是否先转换为1,而是直接整形为NaNconsole.log(parseInt(true)); //NaNconsole.log(parseInt(false)); //NaNconsole.log(parseInt(null)); //NaNconsole.log(parseInt(undefined)); //NaN
Boolean()
console.log(Boolean(1)); //trueconsole.log(Boolean(null)); //falseconsole.log(Boolean(false)); //falseconsole.log(Boolean(undefined)); //falseconsole.log(Boolean(NaN)); //falseconsole.log(Boolean(0)); //falseconsole.log(Boolean('')); //false
隐式转换
- 加号会将字符串转为数字
- 字符串加数字转为字符串
- 乘除余减会将字符串转数字
- 比较运算符将字符串转数字
内置对象
JS的原生标准内置对象:
- 原始值:
undefined,null,number,string,boolean - 引用值:
object,array,function - 包装对象:
Number,String,Boolean - 构造函数:
Object,Function,Date,Error,RegExp,Array - 内置对象:
Math,JSON,Arguments
宿主对象
宿主环境提供给我们的扩展对象,接口:
window,document,history,navigator
WebsAPI:POSTmessage,indexDB,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()
