六、Array 扩展

/*
* Array Api Array.of 数组的构建
* */
{
let arr = Array.of(, , , , , );
console.log(arr); //[3,4,5,6,7,8] let empty = Array.of();
console.log(empty); // []
} /*
* Array API Array.from *将伪数组或一些集合转化为真正的数组
* *类似map方法
* */ {
let p = document.querySelectorAll('p'); //拿到的是一个伪数组
let pArr = Array.from(p);
pArr.forEach(function (item) {
console.log(item.textContent); //依次输出每个 p 元素的文本内容
}); console.log(Array.from([, , , , ], function (item) {
return item * ; // 对要构建的数组成员进行处理
}))
// [2,4,6,8,10]
} /*
* Array API Array.fill(*,startIndex,endIndex) 数组的填充
* */ {
console.log('fill-5', [, , , undefined].fill()); //[5,5,5,5] console.log('fill,pos', ['a', 'b', 'c', 'd', 'e'].fill(, , )); //[a,7,7,d,e] *含头不含尾
} /*
* Array 遍历相关API keys()
* values()[需要编译,最新版的Chrome还未支持]
* entries()
* */
{
for (let index of ['', 'a', 'b', ''].keys()) {
console.log(index); //0 1 2 3
} //for (let v of ['1', 'a', 'b', '2'].values()) {
// console.log(v); //1 2 b 2
//} for (let [index,value] of ['', 'a', 'b', ''].entries()) {
console.log(index, value); //0 1 1 a 2 b 3 2
}
} /*
* Array API Array.copyWithin(target, start, end = this.length)
* 最后一个参数为可选参数,省略则为数组长度。
* 该方法在数组内复制从start(包含start)位置到end(不包含end)位置的一组元素覆盖到以target为开始位置的地方
* */ {
console.log([, , , , ].copyWithin(, , )); //[4,5,3,4,5]
//将 5>index>=3的成员 4,5
//从 index=0的位置开始覆盖
} /*
* Array API Array.find(fun) *找出符合fun条件的 第一个 数组中成员的值
* Array.find(fun) *找出符合fun条件的 第一个 数组中成员的下标
* Array.includes(target) *寻找数组中是否包含 target **可以处理NaN
* */
{
console.log([, , , , , ].find(function (item) { //
return item > ;
})); console.log([, , , , , ].findIndex(function (item) { //
return item > ;
})); console.log([,,NaN].includes()); //true console.log([,,NaN].includes(NaN)); //true *ES5中 NaN!=NaN
}

七、function 扩展      *尾调用相关  参考:http://www.ruanyifeng.com/blog/2015/04/tail-call.html

/*
* 函数参数默认值
* */ {
function test(x, y = 'world') { // *如果 参数y 不存在 则为y指定默认值
console.log(x, y);
} test('hello'); //hello world
test('hello', 'kill'); //hello kill function testError(x, y = 'world', c) { // *在设置了默认值的参数后面不能再有没有默认值的变量
// ** 错误写法
}
} /*
* 函数作用域 ... rest参数 把离散的值变成一个数组
* */ {
let x = 'test'; function test2(x, y = x) { // * 作用域存在x时 y的取值
console.log('作用域', x, y);
} test2('kill'); //kill kill
test2(); //'作用域', undefined undefined function test3(c, y = x) { // * 作用域不存在x时 y的取值
console.log('作用域', c, y);
} test3('kill'); //'作用域', kill test function test4(...arg) { //* rest参数后面不能再有其他的参数
for (let v of arg) {
console.log('rest', v);
}
} test4(1, 2, 3, 4, 'a'); //依次输出了所以的参数
} /*
* 扩展运算符 将一个数组变成 离散的值
* */ {
console.log(...[1, 2, 4]); //1 , 2 , 4
} /*
* 箭头函数 函数名 = 参数 => 返回值
* */ {
let arrow = v=>v * 2;
console.log('arrow', arrow(2)); //arrow 4 let arrow2 = ()=>5;
console.log('arrow2', arrow2()); //'arrow2' 5
} /*
* 尾调用 某个函数的最后一步是调用另一个函数。
* 提升性能 解决函数地址嵌套
* */ {
function tail(x) {
console.log('tail', x);
} function fx(x) {
return tail(x);
}
fx(123); //tail 123
}

八、Object 扩展

/*
* obj 简洁表示法
* */ {
let o =1;
let k =2;
let es5 = {
o:o,
k:k
}
let es6 = {
o,
k
}
console.log(es5,es6); let es5_method = {
hello:function(){
console.log('hello')
}
}
let es6_method ={
hello(){
console.log('hello');
}
}
console.log(es5_method.hello(),es6_method.hello());
} /*
* 属性表达式
* */ {
let a= 'b';
let es5_obj = {
a:'c'
} let es6_obj={
[a]:'c' //[]表达式 使用变量作为key b:'c'
} console.log(es5_obj,es6_obj);
} /*
* Obj API Object.is(arg1,arg2) 判断两个参数是否相等 功能上等于 ===
* */ {
console.log('String',Object.is('abc','abc')); //true
console.log('Array',Object.is([],[]),[]===[]) //false false 对于引用类型判断和 === 一致
} /*
* Obj API Object.assign(源对象,要拷贝到的对象) 对象的拷贝 *浅拷贝 只修改引用地址 不会拷贝继承和不可枚举的属性
* */ {
console.log('拷贝',Object.assign({a:'c'},{b:'b'})); //{a:'c',b:'b'}
} /*
* Obj API Object.entries(obj) 对象遍历法
* */ {
let test = {a:'a',b:'b'};
for(let [key,value] of Object.entries(test)){
console.log(key,value);
}
} /*
* Obj 扩展运算符
* */ {
let {a,b,...c}={a:'test',b:'bbb',c:'ccc',d:'ddd'}
console.log(c); //{c:'ccc',d:'ddd'}
}

ES6新特性使用小结(二)的更多相关文章

  1. ES6新特性使用小结(三)

    九.数据类型 Symbol /* * Symbol 数据类型 概念: Symbol 提供一个独一无二的值 * */ { let a1 = Symbol(); let a2 = Symbol(); co ...

  2. ES6新特性使用小结(六)

    十三.promise 异步编程 ①.使用 promise 模拟异步操作 { //ES5 中的 callback 解决 异步操作问题 let ajax = function (callback) { c ...

  3. ES6新特性使用小结(一)

    一.let const 命令 'use strict'; /*function test(){ //let a = 1; for(let i=1;i<3;i++){ console.log(i) ...

  4. ES6新特性使用小结(五)

    十二.class 与 extends ①.类的基本定义和生成实例 { class Parent{ constructor(name='Lain'){ //定义构造函数 this.name = name ...

  5. ES6新特性使用小结(四)

    十一.Proxy .Reflect ①.Proxy 的概念和常用方法 { let obj = { //1.定义原始数据对象 对用户不可见 time: '2017-09-20', name: 'net' ...

  6. Atitit js版本es5 es6新特性

    Atitit js版本es5 es6新特性 Es5( es5 其实就是adobe action script的标准化)1 es6新特性1 Es5( es5 其实就是adobe action scrip ...

  7. 你不知道的JavaScript--Item24 ES6新特性概览

    ES6新特性概览 本文基于lukehoban/es6features ,同时参考了大量博客资料,具体见文末引用. ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代 ...

  8. ES6新特性概览

    本文基于lukehoban/es6features ,同时参考了大量博客资料,具体见文末引用. ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代号harmony( ...

  9. ES6新特性之模板字符串

    ES6新特性概览  http://www.cnblogs.com/Wayou/p/es6_new_features.html 深入浅出ES6(四):模板字符串   http://www.infoq.c ...

随机推荐

  1. 部署nginx支持lua

    nginx yum -y install gcc pcre pcre-devel openssl openssl-devel  GeoIP GeoIP-devel lua lua-develwget ...

  2. VLAN虚拟局域网技术(二)-计算机网络

    本文主要知识来源于学校课程,部分知识来自于H3C及思科中国公司网页技术手册,未经许可,禁止转载.如需转载,请联系作者并注明出处. 本节主要是总结一些思科的VLAN组中的私有协议:DTP和VTP. 1. ...

  3. JXL 的API

    使用Windows操作系统的朋友对Excel(电子表格)一定不会陌生,但是要使用Java语言来操纵Excel文件并不是一件容易的事.在Web应用日益盛行的今天,通过Web来操作Excel文件的需求越来 ...

  4. Digging-贪心

    When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. ...

  5. 在visualstudio中使用Qt

    1.  说明 在此说明一下IDE跟封装的之间的关系,他们之间本质上来说没有关系,是可以多对对的关系. Qt开发是个比较泛的概念,Qt是由很多一系列类组成的整体,就像boost里面也有很多的类,而boo ...

  6. java去任意范围的随机数

    一.java.uitl.Randomrandom.nextInt(20),任意取[0,20)之间整数,其中0可以取到,20取不到 二.取某个范围的任意数public static String get ...

  7. thymeleaf控制view的返回格式

    package com.ailk.dd1.jike.web.config; import nz.net.ultraq.thymeleaf.LayoutDialect; import org.sprin ...

  8. CF-828C

    C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. sql中内联&nbsp;和外联&nbsp;区别

    sql中内联 和外联 区别 2007-05-15 17:37 这个概念一般看书不好理解.其实夜简单.有例子就简单了. 比如: 表A(主表) cardid username 16 aa 23 bb 25 ...

  10. javascript数组对象

    constructor属性 返回数组对象原型 var arr = [1,2,3,4,5]; arr.constructor //输出 function Array() { [native code] ...