function log(x, y = 'World') {
console.log(x, y)
}
log('Hello') // Hello World
log('Hello', 'China') // Hello China
复制代码
function f(...values){
console.log(values.length)
}
f(1,2) //2
f(1,2,3,4) //4
复制代码
//ES5
var f = function (a,b) {
return a+b
}
//ES6
var f = (a,b) => {
return a+b
}
复制代码
遍历器(Iterator)是一种接口,为各类不一样的数据结构提供统一的访问机制。任何数据结构只要部署Iterator接口,就能够完成遍历操做(即依次处理该数据结构的全部成员)。数组
let arr = ['a', 'b', 'c']
let iter = arr[Symbol.iterator]()
iter.next() // { value: 'a', done: false }
iter.next() // { value: 'b', done: false }
iter.next() // { value: 'c', done: false }
iter.next() // { value: undefined, done: true }
复制代码
for...of 是 ES6 新引入的循环,用于替代 for..in 和 forEach() ,而且支持新的迭代协议。它可用于迭代常规的数据类型,如数组、Set 和 Map 结构、某些相似数组的对象(好比arguments对象、DOM NodeList 对象)、Generator 对象以及字符串。bash
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
toString() {
return '(' + this.x + ', ' + this.y + ')'
}
}
复制代码
方法是类的默认方法,经过new命令生成对象实例时,自动调用该方法。一个类必须有constructor方法,若是没有显式定义,一个空的constructor方法会被默认添加。数据结构
生成类的实例的写法,与 ES5 彻底同样,也是使用new命令。函数
class Point {
// ...
}
var point = new Point(2, 3)
复制代码
在“类”的内部可使用get和set关键字,对某个属性设置存值函数和取值函数,拦截该属性的存取行为。ui
class Point {
constructor(x = 1) {
this.x = x
}
get xx() {
return this.x
}
set xx(val) {
this.x = val
}
}
const point = new Point()
point.xx = 99
console.log(point.x) //99
复制代码
若是在一个方法前,加上static关键字,就表示该方法不会被实例继承。this
class Foo {
static classMethod() {
return 'hello'
}
}
Foo.classMethod() // 'hello'
var foo = new Foo()
foo.classMethod()
// TypeError: foo.classMethod is not a function
复制代码
父类的静态方法,能够被子类继承。spa
class Foo {
static classMethod() {
return 'hello'
}
}
class Bar extends Foo {
}
Bar.classMethod() // 'hello'
复制代码
实例属性除了定义在constructor()方法里面的this上面,也能够定义在类的最顶层。rest
class foo {
bar = 'hello'
baz = 'world'
constructor() {
// ...
}
}
复制代码
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y) // 调用父类的constructor(x, y)
this.color = color
}
toString() {
return this.color + ' ' + super.toString() // 调用父类的toString()
}
}
复制代码
Object.getPrototypeOf方法能够用来从子类上获取父类。code
class A {}
class B extends A {
constructor() {
super()
}
}
复制代码
class A {
p() {
return 2
}
}
class B extends A {
constructor() {
super()
console.log(super.p()) // 2
}
}
let b = new B()
复制代码
原生构造函数的继承对象
ECMAScript 的原生构造函数大体有下面这些:
/*-----export [test.js]-----*/
let myName = "Tom"
let myAge = 20
let myfn = function(){
return "My name is" + myName + "! I'm '" + myAge + "years old."
}
let myClass = class myClass {
static a = "yeah!"
}
export { myName, myAge, myfn, myClass }
/*-----import [xxx.js]-----*/
import { myName, myAge, myfn, myClass } from "./test.js"
console.log(myfn())// My name is Tom! I'm 20 years old. console.log(myAge)// 20 console.log(myName)// Tom console.log(myClass.a )// yeah! 复制代码
export 命令导出的接口名称,须和模块内部的变量有一一对应关系。
导入的变量名,须和导出的接口名称相同,即顺序能够不一致。
/*-----export [test.js]-----*/
let myName = "Tom"
export { myName as exportName }
/*-----import [xxx.js]-----*/
import { exportName } from "./test.js"
console.log(exportName)// Tom
使用 as 从新定义导出的接口名称,隐藏模块内部的变量
/*-----export [test1.js]-----*/
let myName = "Tom"
export { myName }
/*-----export [test2.js]-----*/
let myName = "Jerry"
export { myName }
/*-----import [xxx.js]-----*/
import { myName as name1 } from "./test1.js"
import { myName as name2 } from "./test2.js"
console.log(name1)// Tom
console.log(name2)// Jerry
复制代码
import {a} from "./xxx.js"
a = {} // error
import {a} from "./xxx.js"
a.foo = "hello" // a = { foo : 'hello' }
复制代码
import { a } "./xxx.js"
import { a } "./xxx.js"
// 至关于 import { a } "./xxx.js"
import { a } from "./xxx.js"
import { b } from "./xxx.js"
// 至关于 import { a, b } from "./xxx.js"
复制代码
import { "f" + "oo" } from "methods"
// error
let module = "methods"
import { foo } from module
// error
if (true) {
import { foo } from "method1"
} else {
import { foo } from "method2"
}
// error
复制代码
var a = "My name is Tom!"
export default a
export default var c = "error"
// error,default 已是对应的导出变量,不能跟着变量声明语句
import b from "./xxx.js" // 不须要加{}, 使用任意变量接收
复制代码