六、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. 对于glut和freeglut的一点比较和在VS2013上的配置问题

    先大概说一下glut.h和freeglut.h 首先要知道openGL是只提供绘图,不管窗口的,所以你需要给它一个绘图的区域(openGL能跨平台也与此有些关系) glut.h和freeglut.h都 ...

  2. java--List判断是否为空

    list.isEmpty()和list.size()==0 没有区别 isEmpty()判断有没有元素,size()返回元素个数 如果判断一个集合有无元素,用isEmpty()方法. 这就相当与,你要 ...

  3. c++的最小整数和最大整数

    #include<iostream> #include<cmath> using namespace std; int main() { //int -2147483648~2 ...

  4. 机器学习(九)—逻辑回归与SVM区别

    逻辑回归详细推导:http://lib.csdn.net/article/machinelearning/35119 面试常见问题:https://www.cnblogs.com/ModifyRong ...

  5. Smack编程库进行代码功能调试

    http://www.tuicool.com/articles/U3Afiy 使用Smack编程库进行代码功能调试 关于Smack编程库,前面我们提到,它是面向Java端的api,主要在PC上使用,利 ...

  6. EmbarassedBirds全体开发人员落泪

    Github (李昆乘,赖展飞) 现阶段还在开发后期,API调试过程中. 本周无法上线. 全体开发人员留下眼泪. 贴上几个功能图, 给大家尝尝鲜吧! 现阶段仍在API调试 因为队员李昆乘经常出去玩没有 ...

  7. hdu 6103(Kirinriki)

    题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...

  8. codeforces 558B B. Amr and The Large Array(水题)

    题目链接: B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes in ...

  9. 「P3385」【模板】负环(spfa

    题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...

  10. 【Opencv】Mat基础

    1.Mat::imread() C++: Mat imread(const string& filename, int flags=1 ) filename – Name of file to ...