Class 的基本语法
ES5写法
function Point(x,y){this.x = xthis.y = y}Point.prototype.toString = function(){return `${this.x} + ${this.x}`}var p = new Point(1,2)console.log(p)console.log(p.toString())
ES6
class Point{constructor(x,y){this.x = x;this.y = y}toString(){return `${this.x} + ${this.x}`}}
constructor 方法
解构赋值
用途
交换变量的值
let x = 1;let y = 2;[x, y] = [y, x];
从函数返回多个值
// 返回一个数组function example() {return [1, 2, 3];}let [a, b, c] = example();// 返回一个对象function example() {return {foo: 1,bar: 2};}let { foo, bar } = example();
提取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]
函数参数的默认值
jQuery.ajax = function (url, {async = true,beforeSend = function () {},cache = true,complete = function () {},crossDomain = false,global = true,// ... more config} = {}) {// ... do stuff};
指定参数的默认值,就避免了在函数体内部再写var foo = config.foo || 'default foo';这样的语句。
输入模块的指定方法
指定输入哪些方法
const { SourceMapConsumer, SourceNode } = require("source-map");
