es6新增功能
声明命令
|
1
2
3
4
5
6
|
{ let a = 10; var b = 1;}console.log(b); // 1console.log(a); // 报错a没有定义 |
|
1
2
3
4
|
for (let i = 0; i < 10; i++) { // ...}console.log(i); // 报错i没有定义 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
var a = [];for (var i = 0; i < 10; i++) { a[i] = function () { console.log(i); };}a[6](); // 10var a = [];for (let i = 0; i < 10; i++) { a[i] = function () { console.log(i); };}a[6](); // 6 |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
let a = 10;// 即使声明是var a = 10;后面一样报错let a = 1;// 报错function func(arg) { let arg; // 调用时报错}function func(arg) { { let arg; // 不报错,因为对上一个arg来看在子模块中 }} |
|
1
2
3
4
5
6
7
8
9
10
11
|
let i = 123;console.log(i);for (let i = 0; i < 2; i++,console.log(i)) { let i = 'abc'; console.log(i);}// 123// abc// 1// abc// 2 |
|
1
2
|
typeof x; // 报错:同一块作用域在let x之前,x无法进行任何操作let x; |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
function text1(){ let n = 5; //或var n = 5; if (true) { let n = 10; } console.log(n); // 5}function text2(){ var n = 5; if (true) { var n = 10; } console.log(n); // 10}function text3(){ let n = 5; if (true) { var n = 10; //报错,已经声明了n }} |
|
1
2
3
4
|
const PI = 3.1415;PI = 3; //报错,赋值给常量const a; //报错,缺少初始化 |
|
1
2
3
4
|
const foo = {}; // const foo = []同理,可以正常使用push等功能foo.prop = 123; // 为foo添加一个属性,可以成功console.log(foo.prop); //123foo = {}; // 将foo指向另一个对象,就会报错 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
function Point(x, y) { this.x = x; this.y = y;}Point.prototype.toString = function () { return '(' + this.x + ', ' + this.y + ')';};// 上面为原先写法,下面为ES6的Class写法class Point { constructor(x, y) { // 构造方法,this关键字代表实例对象 this.x = x; this.y = y; } toString() { // 自定义方法,方法之间不需要逗号分隔,加了会报错 return '(' + this.x + ', ' + this.y + ')'; }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Point { constructor() { } toString() { } toValue() { }}console.log(Object.keys(Point.prototype)); // [] 不可枚举console.log(Object.getOwnPropertyNames(Point.prototype)); // ["constructor", "toString", "toValue"]// 相当于function Point() {}Point.prototype = { constructor() {}, toString() {}, toValue() {},};console.log(Object.keys(Point.prototype)); // ["constructor", "toString", "toValue"] |
|
1
2
3
4
5
|
let methodName = 'getArea';class Square { [methodName]() { }} |
|
1
2
3
4
5
6
7
8
|
const MyClass = class Me { // 如果类的内部没用到的话,可以省略Me getClassName() { return Me.name; }};let inst = new MyClass();console.log(inst.getClassName()) // MeMe.name // 报错,Me没有定义 |
|
1
2
3
4
5
6
7
8
|
class Foo { static classMethod() { return 'hello'; }}Foo.classMethod() // 'hello'var foo = new Foo();foo.classMethod() // 报错foo.classMethod不是一个函数(不存在该方法) |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Foo { static bar () { this.baz(); //等同于调用Foo.baz } static baz () { console.log('hello'); } baz () { console.log('world'); }}Foo.bar() // helloclass Bar extends Foo {}Bar.classMethod() // hello |
|
1
2
3
4
5
6
7
8
9
10
|
// profile.jsexport var firstName = 'Michael';export function f() {};export var year = 1958;//写法2,与上等同var firstName = 'Michael';function f() {};var y = 1958;export {firstName, f, y as year}; |
|
1
2
3
4
5
6
|
export var foo = 'bar';setTimeout(() => foo = 'baz', 500); // 输出变量foo,值为bar,500毫秒之后变成bazfunction foo() { export default 'bar' // 语法错误} |
|
1
2
3
4
5
6
|
import {firstName as name, f, year} from './profile.js';import * as p from './profile.js'; function setName(element) { element.textContent = name + ' ' + year; // 值等同于p.firstName + ' ' + p.year;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import {a} from './xxx.js'; // 也可以是绝对路径,.js后缀可以省略a.foo = 'hello'; // 合法操作a = {}; // 报错:a是只读的import { 'f' + 'oo' } from '/my_module.js';// 报错,语法错误(不能用运算符)if (x === 1) { import { foo } from 'module1'; // 报错,语法错误(import不能在{}内)} else { import { foo } from 'module2';} |
|
1
2
3
4
|
foo();import { foo } from '/my_module.js'; // 不会报错,因为import的执行早于foo的调用import '/modules/my-module.js'; // 不引入变量,但执行其中全局代码import { a } from '/modules/my-module.js'; // 重复引入不执行全局代码,但引入变量a |
|
1
2
3
4
5
6
7
8
|
// export-default.jsexport default function () { console.log('foo');}// import-default.jsimport customName from './export-default.js'; //customName可以是任意名字customName(); // 'foo' |
解构赋值
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
let a = 1;let b = 2;let c = 3;// 等价于let [a, b, c] = [1, 2, 3];let [ , third] = ["foo", "bar", "baz"];third // "bar"let [head, ...tail] = [1, 2, 3, 4];head // 1tail // [2, 3, 4]let [x, y, ...z] = ['a'];x // "a"y // 变量解构不成功,赋值为undefinedz // 数组解构不成功,赋值为[] |
|
1
2
3
4
|
let [foo = true] = []; // foo = truelet [x, y = 'b'] = ['a']; // x='a', y='b'let [q = 1, w = 'b'] = ['a', undefined]; // q='a', w='b'let [e = 1] = [null]; // e = null |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
let { bar, foo } = { foo: "aaa", bar: "bbb" };foo // "aaa"bar // "bbb"let { abc } = { foo: "aaa", bar: "bbb" };abc // undefinedlet { foo: baz } = { foo: 'aaa', bar: 'bbb' };baz // "aaa"const node = { loc: { start: { line: 1, column: 5 } }};let { loc, loc: { start }, loc: { start: { line }} } = node;line // 1loc // Object {start: Object}start // Object {line: 1, column: 5} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//交换变量的值let x = 1;let y = 2;[x, y] = [y, x]; //提取 JSON 数据let jsonData = { id: 42, status: "OK", data: [867, 5309]};let { id, status, data: number } = jsonData;console.log(id, status, number); // 42, "OK", [867, 5309]//遍历 Map 结构const map = new Map();map.set('first', 'hello');map.set('second', 'world');for (let [key, value] of map) { console.log(key + " is " + value);}// first is hello// second is world |
兼容问题
|
1
2
3
4
5
6
7
|
/// 转码前input.map(item => item + 1);// 转码后input.map(function (item) { return item + 1;}); |
|
1
2
3
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//引用外部ES6的js<script type="module"> import './Greeter.js';</script>//直接写ES6的JS<script type="module"> class Calc { constructor() { console.log('Calc constructor'); } add(a, b) { return a + b; } } var c = new Calc(); console.log(c.add(4,5)); //正常情况下,会在控制台打印出9。</script> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<script> // 创建系统对象 window.System = new traceur.runtime.BrowserTraceurLoader(); // 设置参数 var metadata = { traceurOptions: { experimental: true, properTailCalls: true, symbols: true, arrayComprehension: true, asyncFunctions: true, asyncGenerators: exponentiation, forOn: true, generatorComprehension: true } }; // 加载模块 System.import('./myModule.js', {metadata: metadata}).catch(function(ex) { console.error('Import failed', ex.stack || ex); });</script> |
es6新增功能的更多相关文章
- ECMAScript简介以及es6新增语法
ECMAScript简介 ECMAScript与JavaScript的关系 ECMAScript是JavaScript语言的国际化标准,JavaScript是ECMAScript的实现.(前者是后者的 ...
- 【ES6新增语法详述】
目录 1. 变量的定义 let const 2. 模版字符串 3. 数据解构 4. 函数扩展 设置默认值 箭头函数 5. 类的定义 class 6. 对象的单体模式 "@ ES6新增了关于变 ...
- ADO.NET 中的新增功能
ADO.NET 中的新增功能: .NET Framework (current version) 以下是 .NET Framework 4.5 中 ADO.NET 的新增功能. SqlClient D ...
- .NET Framework3.0/3.5/4.0/4.5新增功能摘要
Microsoft .NET Framework 3.0 .NET Framework 3.0 中增加了不少新功能,例如: Windows Workflow Foundation (WF) Windo ...
- 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能
[源码下载] 与众不同 windows phone (40) - 8.0 媒体: 音乐中心的新增功能, 图片中心的新增功能, 后台音乐播放的新增功能 作者:webabcd 介绍与众不同 windows ...
- PHP V5.2 中的新增功能,第 1 部分: 使用新的内存管理器
PHP V5.2:开始 2006 年 11 月发布了 PHP V5.2,它包括许多新增功能和错误修正.它废止了 5.1 版并被推荐给所有 PHP V5 用户进行升级.我最喜欢的实验室环境 —— Win ...
- WPF4.5 中的新增功能和增强功能的信息
本主题包含有关 Windows Presentation Foundation (WPF) 版本 4.5 中的新增功能和增强功能的信息. 本主题包含以下各节: 功能区控件 改善性能,当显示大时设置分组 ...
- .NET Framework 4.5、4.5.1 和 4.5.2 中的新增功能
.NET Framework 4.5.4.5.1 和 4.5.2 中的新增功能 https://msdn.microsoft.com/zh-cn/library/ms171868.aspx
- openstack【Kilo】汇总:包括20英文文档、各个组件新增功能及Kilo版部署
OpenStack Kilo版本发布 20英文文档OpenStack Kilo版本文档汇总:各个操作系统安装部署.配置文档.用户指南等文档 Kilo版部署 openstack[Kilo]入门 [准备篇 ...
随机推荐
- J - Clairewd’s message HDU - 4300(扩展kmp)
题目链接:https://cn.vjudge.net/contest/276379#problem/J 感觉讲的很好的一篇博客:https://subetter.com/articles/extend ...
- HTML 解析 textarea 中的换行符
用户在textarea中输入的换行符,传到后台,再返回前端,展示在div中. 如果需要div显示为与textarea 一致的效果,需添加: .detail { white-space: pre-lin ...
- java创建并配置多module的maven项目
1 使用idea创建(推荐) 这篇博客写的特别好,很详细: https://blog.csdn.net/sinat_30160727/article/details/78109769 2 使用ecli ...
- Docker基础速成(一)
Docker基础速成(一) 给亲爱的写的docker基础速成,按照步骤操作,实践出真知,希望有提纲挈领之功效 1.docker简介 Docker 轻量级容器,如图,类似于一个个集装箱,把复杂或者零散的 ...
- 微信web开发者工具无法打开的解决方法
参考网址:https://blog.csdn.net/gz506840597/article/details/77915488 我试了上面兄弟说的方法还是无效 下面说说我的方法: 我打开文件所在位置, ...
- 使用离线包部署kubernetes 1.9.0、kubernetes-dashboard 1.8
=============================================== 2018/3/22_第2次修改 ccb_warlock 更新 ...
- 26 About the go command go命令行
About the go command go命令行 Motivation Configuration versus convention Go's conventions Getting star ...
- PHP获得用户的真实IP地址
<?php /** * 获得用户的真实IP地址 * * @access public * @return string */ function real_ip() { static $reali ...
- python基础-类的起源
Python中一切事物都是对象. class Foo(object): def __init__(self,name): self.name = name f = Foo("alex&quo ...
- 区间DP小结
也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...