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]入门 [准备篇 ...
随机推荐
- tmux终端工具
本文原始地址:http://www.cnblogs.com/chinas/p/7094172.html,转载请注明出处,谢谢!!! 1.介绍 tmux(终端复用工具):一个很有趣的工具,类似GNU S ...
- Django框架下的小人物--Cookie
1. 什么是Cookie,它的用途是什么? Cookies是一些存储在用户电脑上的小文件.它是被设计用来保存一些站点的用户数据,这样能够让服务器为这样的用户定制内容,后者页面代码能够获取到Cookie ...
- java_环境安装(window10)
参考地址 下载JDK 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 本地环境变 ...
- 关于oracle数据库死锁的检查方法
一.数据库死锁的现象程序在执行的过程中,点击确定或保存按钮,程序没有响应,也没有出现报错. 二.死锁的原理当对于数据库某个表的某一列做更新或删除等操作,执行完毕后该条语句不提交,另一条对于这一列数据做 ...
- 【Andorid开发框架学习】之Volley入门
Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮.Volley特别适合数据量不大但是通信频繁的场景.在listView显示图片这方面,使用volley也是比较好的,不必 ...
- rsync + inotify 实时同步
1. 前言 2 台 nginx 需要做集群, 静态文件和php文件都在nginx服务器本地. 有三种方案: (1)NFS (2)Rsync + inotify (3)共享存储服务器 第一种:当 nfs ...
- file.getPath() getAbsolutePath() getCanonicalPath()区别
package file; import java.io.File; import java.io.IOException; public class getFilePath { public sta ...
- IPv4的核心管理功能/proc/sys/net/ipv4/*
I /proc/sys/net/ipv4/tcp_syncookies SYN Cookies模块可以在系统随机端口(1024:65535)即将用完时自动启动,用来应对Dos攻击.当启动SYN Coo ...
- Filebeat入门
一.安装filebeat 简介 Beats 是安装在服务器上的数据中转代理. Beats 可以将数据直接传输到 Elasticsearch 或传输到 Logstash . Beats 有多种类型,可以 ...
- iOS中URL的解码和转义问题
在iOS开发中,使用NSURLConnection去请求google places api时,如果请求的url中包含中文,则返回的结果为空,URL不能被google识别.NSString *_urlS ...