ES6 核心特性

块级作用域

let : 声明的变量存在块级作用域  不会声明提前

ES5

// ES5 
// 声明提前
var x = 'outer';
function test(inner) {
if (inner) {
var x = 'inner';
console.log(x);
}
console.log(x);
}
test(false) //undefined
test(true) // inner inner

ES6

// ES6
// 声明不提前
let x = 'outer';
function test(inner) {
if (inner) {
let x = 'inner';
console.log(x);
}
console.log(x);
}
test(false) // outer
test(true) // inner outer

优点

// ES5
{
var a = 1;
}
console.log(a)
// ES6
{
let b = 2;
}
console.log(b)

const : 常量  不可以修改


模板字符串

使用 ` ` 包裹  变量使用${}

// ES5
var str1 = 'lpt';
var str2 = 'want to eat everything!';
console.log('我想说的是:' + str1 + ' ' + str2)
// ES6
const str3 = 'lpt';
const str4 = 'want to eat everything!';
console.log(`我想说的是:${str3} ${str4}`)

解构复制

解构赋值允许你使用类似数组或对象字面量的语法将数组和对象的属性赋给各种变量

如果默认值是一个函数,那么函数只会在有需要才会去求值

function fn(num){
console.log(num);
return num;
}
let [a = fn(1)] = [10]; // 不执行函数
let [b = fn(2)] = []; // 执行函数
a //
b //

解构赋值允许指定默认值

// ES5
var arr = [1, 2, 3, 4];
var first = arr[0];
var third = arr[2];
console.log(first, third); // 1 3
// ES6
const arr1 = [1, 2, 3, 4];
const [a, ,c=9] = arr1;
console.log(a,c)

交换value

// ES5
var a = 1;
var b = 2;
var tmp = a;
a = b;
b = tmp;
console.log(a, b); // 2 1
// ES6
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b); // 2 1

解构为多个返回值

// ES6
function margin() {
const left=1, right=2, top=3, bottom=4;
return { left, right, top, bottom };
}
const { left, bottom } = margin();
console.log(left, bottom); // 1 4

类和对象

// ES5
var Animal = (function () {
function MyConstructor(name) {
this.name = name;
}
MyConstructor.prototype.speak = function speak() {
console.log(this.name + ' makes a noise.');
};
return MyConstructor;
})();
var animal = new Animal('lpt');
animal.speak(); // lpt makes a noise. // ES6
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
const animal = new Animal('lpt');
animal.speak(); // lpt makes a noise.

继承

// ES5
var Animal = (function () {
function MyConstructor(name) {
this.name = name;
}
MyConstructor.prototype.speak = function speak() {
console.log(this.name + ' makes a noise.');
};
return MyConstructor;
})();
var Monkey = (function () {
function MyConstructor(name){
Animal.call(this, name);
}
// prototypal inheritance
MyConstructor.prototype = Object.create(Animal.prototype);
MyConstructor.prototype.constructor = Animal;
MyConstructor.prototype.speak = function speak() {
Animal.prototype.speak.call(this);
console.log(this.name + ' roars');
};
return MyConstructor;
})();
var monkey = new Monkey('Simba');
monkey.speak();
// Simba makes a noise.
// Simba roars. // ES6
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Lion extends Animal {
speak() {
super.speak();
console.log(this.name + ' roars');
}
}
const lion = new Lion('Simba');
lion.speak();
// Simba makes a noise.
// Simba roars.

箭头函数

箭头函数完全修复了this的指向,this总是指向词法作用域,也就是外层调用者obj

// ES6
var obj = {
birth: 1992,
getAge: function () {
var b = this.birth; //
var fn = () => new Date().getFullYear() - this.birth; // this指向obj对象
console.log( fn() );
}
};
obj.getAge(); //

For…of

// for
var array = ['a', 'b', 'c', 'd'];
for (var i = 0; i < array.length; i++) {
var element = array[i];
console.log(element);
}
// forEach
array.forEach(function (element) {
console.log(element);
});
// for …of
for (const element of array) {
console.log(element);
}

默认参数

// ES5
function point(x, y, isFlag){
x = x || 0;
y = y || -1;
isFlag = isFlag || true;
console.log(x,y, isFlag);
}
point(0, 0) // 0 -1 true
point(0, 0, false) // 0 -1 true
point(1) // 1 -1 true
point() // 0 -1 true
// ES6
function point(x = 0, y = -1, isFlag = true){
console.log(x,y, isFlag);
}
point(0, 0) // 0 0 true
point(0, 0, false) // 0 0 false
point(1) // 1 -1 true
point() // 0 -1 true

求数组最大值

Math.max(...[2,100,1,6,43]) //

使用扩展运算符(...)拷贝数组

// bad
const len = items.length;
const itemsCopy = [];
let i; for (i = 0; i < len; i++) {
itemsCopy[i] = items[i];
} // good
const itemsCopy = [...items];

使用Array.from方法,将类似数组的对象转为数组

const foo = document.querySelectorAll('.foo');
const nodes = Array.from(foo);

未完待续

JavaScript ES6特性的更多相关文章

  1. ES6:JavaScript 新特性

    我相信,在ECMAScript.next到来的时候,我们现在每天都在写的JavaScript代码将会发生巨大的变化.接下来的一年将会是令JavaScript开发者们兴奋的一年,越来越多的特性提案将被最 ...

  2. JavaScript ES6 新特性详解

    JavaScript ES6 带来了新的语法和新的强大功能,使您的代码更现代,更易读 const ,  let and var 的区别: const , let 是 ES6 中用于声明变量的新关键字. ...

  3. 最常用的ES6特性(转)

    最常用的ES6特性 let, const, class, extends, super, arrow functions, template string, destructuring, defaul ...

  4. ES6特性

    一.ES6特性: let, const, class, extends, super, arrow functions, template string, destructuring, default ...

  5. 逆转序列的递归/尾递归(+destructuring assignment)实现(JavaScript + ES6)

    这里是用 JavaScript 做的逆转序列(数组/字符串)的递归/尾递归实现.另外还尝鲜用了一下 ES6 的destructuring assignment + spread operator 做了 ...

  6. 第四节:教你如何快速让浏览器兼容ES6特性

    写在正文前,本来这一节的内容应该放在第二节更合适,因为当时就有同学问ES6的兼容性如何,如何在浏览器兼容ES6的特性,这节前端君会介绍一个抱砖引玉的操作案例. 为什么ES6会有兼容性问题? 由于广大用 ...

  7. [转]JavaScript ES6 class指南

    [转]JavaScript ES6 class指南 前言 EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建 ...

  8. 解决浏览器兼容ES6特性

    为什么ES6会有兼容性问题? 由于广大用户使用的浏览器版本在发布的时候也许早于ES6的定稿和发布,而到了今天,我们在编程中如果使用了ES6的新特性,浏览器若没有更新版本,或者新版本中没有对ES6的特性 ...

  9. JavaScript ES6箭头函数指南

    前言 胖箭头函数(Fat arrow functions),又称箭头函数,是一个来自ECMAScript 2015(又称ES6)的全新特性.有传闻说,箭头函数的语法=>,是受到了CoffeeSc ...

随机推荐

  1. MySQL在线更改binlog格式

    今天变更jboss报错如下: SQLWarning ignored: SQL state ', message [Unsafe statement written to the binary log ...

  2. Python3学习笔记25-logging模块

    logging模块,Python自带用来记录日志的模块. 因为工作需要用到关于日志的,最近一直都在看关于日志模块的东西,百度了很多文章,可惜都是看的让人一头雾水,最后运气不错,找到一篇很详细的文章.传 ...

  3. mq命令帮助文档

    https://www.ibm.com/support/knowledgecenter/zh/SSFKSJ_7.5.0/com.ibm.mq.ref.adm.doc/q083180_.htm

  4. spring aop的五种通知类型

    昨天在腾讯课堂看springboot的视频,老师随口提问,尼玛竟然回答错了.特此记录! 问题: Spring web项目如果程序启动时出现异常,调用的是aop中哪类通知? 正确答案是: 异常返回通知. ...

  5. springmvc和mybatis整合关键配置

    springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在spr ...

  6. jmeter之正则表达式

    一.Jmeter关联的方式: Jmeter中关联可以在需要获取数据的请求上 右键-->后置处理器 选择需要的关联方式,如下图有很多种方法可以提取动态变化数据: 二.正则表达式提取器: 1.比如需 ...

  7. 淘淘商城 本地仓库配置和仓库jar包下载

    SVN服务器的搭建请查看该文:<Win7 x64 svn 服务器搭建> 1:仓库包存放位置: 2:setting.xml 文件配置信息 <?xml version="1.0 ...

  8. 配置本地无密码 SSH登录远程服务器

    下面这幅图简单来说就是你本地有一把钥匙,服务器也有一把钥匙,当登录的时候本地的钥匙与服务器的进行对比,通过算法的判定,监测是否具有权限的用户 第一步,在本地配置这把钥匙生成私钥与公钥: 打开.ssh目 ...

  9. LeetCode(37): 每k个一组翻转链表

    Hard! 题目描述: 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一 ...

  10. inoremap nnoremap vnoremap

    原贴:https://www.xuebuyuan.com/zh-hant/1116162.html inoremap nnoremap vnoremap i insert 在插入模式有效 n 在 普通 ...