解构赋值的规则:ajax
let {toString: s} = 123; s === Number.proptotype.toString // true let {toString: s} = true; s === Boolean.prototype.toString // true
function add([x,y]){ retrun x + y; } add([1,2]) // 3
函数add的参数实际上不是一个数组,而是经过解构获得的变量x和y。json
函数参数的解构依旧可使用默认值数组
function move({x = 0, y = 0} = {}){ retrun [x,y] } move({x: 3,y: 8}); // [3,8]; move({x: 3}) // [3,0] move({}) // [0,0] move() // [0,0]
函数move的参数是一个对象,经过对这个对象进行解构,获得变量x和y的值。若是解构失败,则返回默认值。async
下面的写法,会存在一些问题
function move({x,y} = {x: 0,y: 0}){ return [x,y]; } move({x: 3,y: 8}); // [3,8]; move({x: 3}) // [3,undefined] move({}) // [undefined,undefined] move() // [0,0]
若是已经存在赋值,则会获取获取undefind函数
[x,y]=[y,x]
// 返回一个数组 function f(){ return [1,2,3]; } var [a,b,c] = f(); // 返回一个对象 function f1(){ ruturn { foo: 1, bar: 2 } } var {foo, bar} = f1();
function f([x,y,z]){...} f({1,2,3}) function f([x,y,z]){...} f({x:1,z:3,y:2})
var jsonData= { id: 42, status: 'ok', } let {id,status: isok} = jsonData; consoloe.log(id, isok); // 42 "OK"
$.ajax=function (url,( async = true, beforeSend = function(){}, cache = true, complete = function(){}, crossDomain = false, global = true, )){ // TODO }
var map = new Map() ; map.set('first', 'hello'); map.set('sec', 'world'); for(let [key, value] of map){ // 从循环的数值中依次赋值key和value console.log(key + "is" + value) // first is hello // sec is world }
const { SourceMapConsumer, SourceNode} = require("source-map")
var [(a)]=[1]; var [x: {c}]={};
function f([(z)]) = { return z; }
({p: a})={p: 42} ([a]) = [5]
[(b)] = [3]; ({p: (d)} = {})