ES6学习笔记2(基础语法的拓展)
阅读时间:全文 1251 字,预估用时 7 分钟
创作日期:2017-06-30
BEGIN
申明
阮一峰老师的ECMAScript 6学习笔记。
字符串拓展
常用字符串操作函数
indexOf(substr)
,返回字串在父串中的下标,否则返回-1。'hello'.indexOf('ll'); //2
includes(substr, num)
,返回字符串是否为子串的布尔值。'hello'.includes('ll'); //true
startsWith(substr, num)
,返回子字符串是否为父字符串的首部。'hello'.startsWith('hel'); //true
endsWith(substr, num)
,返回子字符串是否为父字符串的尾部。'hello'.endsWith('llo'); //true
repeat(num)
,返回字符串拼接num次后的新字符串。'hello'.repeat(2); //hellohello
padStart(num, str)
,格式化输出,头部填充字符串达到num位。'ct'.padStart(4, '$'); //&&ct
padEnd(num, str)
,格式化输出,尾部填充字符串达到num位。'ct'.padStart(4, '$'); //ct&&
模板字符串
模板字符串通过反引号`标识,模板变量放在${}中,可以是变量、对象、表达式或者函数。
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`
//Hello Bob, how are you today?
数值的拓展
Number对象的拓展
Number.isFinite()
: 检查数值是否为有限数值。Number.isNaN()
: 检查数值是否为NaN。Number.parseInt()
和Number.parseFloat()
: 方法parseInt()和parseFloat()移到Number对象下。Number.isInteger()
: 判断数值是否为整数。Number.EPSILON
: 极小常量2.220446049250313e-16Number.MAX_SAFE_INTEGER
: 数值上限9007199254740991 == Math.pow(2,53)-1Number.MAX_SAFE_INTEGER
: 数值下限-9007199254740991 == -Math.pow(2,53)+1Number.isSafeInteger()
: 判断整数是否在上下限之间。
Math对象的拓展
Math.trunc(num)
: 去除一个数的小数部分留下整数。Math.sign(num)
: 判断数值是正数(1)、负数(-1)、0(0)、-0(-0)或者非数值(NaN)。Math.cbrt(num)
: 计算一个数的立方跟。Math.clz32(num)
: 整数以32位二进制形式存储时的前导0个数。Math.hypot(num1, num2)
: 参数的平方和的平方跟。**
和**=
: 指数运算符,同Math.pow(num1, num2)实现方式不同,因此对于大数据结果会有细微差异。
数组的拓展
Array.from()
: 将含迭代器的类似数组或者含length属性的类似数组转换成真正的数组,拓展运算符...
仅能将含迭代器的类似数组转换。
//如函数中的arguments对象
var args = Array.from(arguments);
//等价于
var args = [...arguments];
//替代map方法
Array.from([1, 2, 3], x => x**2); //[ 1, 4, 9 ]
//等价于
[1, 2, 3].map(x => x**2); //[ 1, 4, 9 ]
//或
Array.from([1, 2, 3]).map(x => x**2); //[ 1, 4, 9 ]
//或
Array.prototype.map.call([1, 2, 3], x => x**2); //[ 1, 4, 9 ]
//将字符串转换为字符数组
Array.from('hello'); //[ 'h', 'e', 'l', 'l', 'o' ]
- `Array.of(x, y, …)’: 将一组值转换为数组。
find()
方法找出第一个符合条件的数组成员,否则返回undefined。
[1, 4, -5, 10].find(value => value<0); //-5
[1, 4, -5, 10].find((value, index, arr) => value<0); //-5
findIndex()
方法找出第一个符合条件的数组成员下标,否则返回-1。
[1, 4, -5, 10].findIndex(value => value<0); //2
[1, 4, -5, 10].findIndex((value, index, arr) => value<0); //2
fill(str, start, end)
方法用来填充数组。
new Array(3).fill('xx', 1, 2); //[, 'xx', ]
['a', 'b', 'c'].fill('x', 1, 3); //['a', 'x', 'x']
keys()
、values()
和entries()
分别遍历数组的键、值及键值对。这三个方法都是返回迭代器类型的,因此可以用for…of…进行取值。
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 'a'
// 1 'b'
注:本地运行values()报错,且这三个方法不适用对象。
7. includes(value, start, end)
: 返回布尔值判断数组是否包含此元素。用法同indexOf()
。
对象的拓展
- 对象中属性简写:
{x, y}
等价于{x: x, y: y}
。 - 对象中方法简写:
method(){}
等价于method: function(){}
,如果方法的值为生成器(Generator)函数,方法名前加上星号\*
。 - 实现对象的setter及getter,在对象的方法前加上get或set。
- 对象中属性名及方法名可以是表达式,需要用中括号
[]
括起来。let obj = {['a' + 'b']: 123}
,需要注意的是:属性名表达式不能与简洁表达式同时使用。 Object.is()
: 比较两个值是否相等。此时Object.is(+0, -0); //false
和Object.is(NaN, NaN); //true
。Object.assign(targetObj, obj1, obj2, ...)
: 用于将对象合并targetObj中,实现的是浅拷贝,合并中出现同名属性,后面的覆盖前面的。Object.getOwnPropertyDescriptor(obj, obj.prop)
: 查看属性的描述对象。Object.getOwnPropertyDescriptors(obj)
: 查看对象所有属性(非继承)的描述对象。- 属性的遍历:
for...in...
: 循环遍历对象自身的和继承的可枚举属性(不含Symbol属性)。Object.keys(obj)
: 返回对象自身的所有可枚举属性的数组(不含继承的和Symbol属性)。Object.getOwnPropertyNames(obj)
: 返回对象自身的所有属性的数组(不含Symbol属性,但包括不可枚举属性)。Object.getOwnPropertySymbols(obj)
: 返回对象自身的所有Symbol属性的数组。Reflect.ownKeys(obj)
: 返回对象自身的所有属性的数组。
Object.setPrototypeOf(obj, proto)
: 设置obj的prototype对象。Object.getPrototypeOf(obj)
: 读取obj的prototype对象。Object.keys(obj)
、Object.values(obj)
及Object.entries(obj)
分别返回对象的可遍历属性(不含继承的)的键名、键值及键值对数组。
var oo = {a:1, b:2};
Object.keys(oo); //[ 'a', 'b' ]
Object.values(oo); //[ 1, 2 ]
Object.entries(oo); //[ [ 'a', 1 ], [ 'b', 2 ] ]
//将对象转换成map结构
var obj = { foo: 'bar', baz: 42 };
var map = new Map(Object.entries(obj)); //Map { foo: "bar", baz: 42 }
?.
: Null传导运算符。用于解决判断属性是否存在的问题。
const firstName = (message && message.body && message.body.user && message.body.user.firstName) || 'default';
//等价于
const firstName = message?.body?.user?.firstName || 'default';
FINISH