当前位置: 首页 > news >正文

西安市建设工程信息网诚信信息平台官网大连seo网站推广

西安市建设工程信息网诚信信息平台官网,大连seo网站推广,太原网站建设需要多少钱,网站底部的备案号变量解构赋值 数组解构赋值1 基操2 默认值 对象的解构赋值默认值注意 字符串的解构赋值数值与布尔值的解构赋值函数参数的解构赋值圆括号不得使用 作用 数组解构赋值 1 基操 ES6允许按照一定的模式从数组和对象中提取值从而对变量进行赋值,也即解构(De…

变量解构赋值

    • 数组解构赋值
      • 1 基操
      • 2 默认值
    • 对象的解构赋值
      • 默认值
      • 注意
    • 字符串的解构赋值
    • 数值与布尔值的解构赋值
    • 函数参数的解构赋值
    • 圆括号
      • 不得使用
    • 作用

数组解构赋值

1 基操

ES6允许按照一定的模式从数组和对象中提取值从而对变量进行赋值,也即解构(Destructuring)
也即为变量赋值可以有下面的方法

let a = 1;
let b = 2;
let c = 3;
// or
let [a, b, c] = [1, 2, 3];

按照对应位置从数组中提取值为变量赋值
此种写法的本质上是等号两边的模式匹配
下面是使用嵌套数组进行解构

let [foo, [[bar], baz]] = [1, [[2], 3]];
foo // 1
bar // 2
baz // 3let [ , , third] = ["foo", "bar", "baz"];
third // "baz"let [x, , y] = [1, 2, 3];
x // 1
y // 3let [head, ...tail] = [1, 2, 3, 4];
head // 1
tail // [2, 3, 4]let [x, y, ...z] = ['a'];
x // "a"
y // undefined
z // [] 

解构不成功则变量的值等于undefined

let [foo] = [];
let [bar, foo] = [1];

上面的两种情况属于结构失败
foo的值为undefined

还有就是不完全解构(但是成功)
等号左边的模式只匹配一部分的等号右边的数组

let [x, y] = [1, 2, 3];
x // 1
y // 2let [a, [b], d] = [1, [2, 3], 4];
a // 1
b // 2
d // 4

如果等号的右边不是数组(或者严格地说不是可遍历的结构)则会报错

// 报错
let [foo] = 1;
let [foo] = false;
let [foo] = NaN;
let [foo] = undefined;
let [foo] = null;
let [foo] = {};

上面的语句都会报错,因为等号右边的值,要么转为对象以后不具备 Iterator 接口(前五个表达式),要么本身就不具备 Iterator 接口(最后一个表达式)

对于 Set 结构,也可以使用数组的解构赋值

let [x, y, z] = new Set(['a', 'b', 'c']);
x // "a"

事实上只要某种数据结构具有 Iterator 接口,都可以采用数组形式的解构赋值

function* fibs() {let a = 0;let b = 1;while (true) {yield a;[a, b] = [b, a + b];}
}let [first, second, third, fourth, fifth, sixth] = fibs();
sixth // 5

上面代码中,fibs是一个 Generator 函数,原生具有 Iterator 接口
解构赋值会依次从这个接口获取值

2 默认值

解构赋值允许指定默认值

let [foo = true] = [];
foo // truelet [x, y = 'b'] = ['a']; // x='a', y='b'
let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'

注意ES6 内部使用严格相等运算符(===),判断一个位置是否有值
所以只有当一个数组成员严格等于undefined,默认值才会生效

let [x = 1] = [undefined];
x // 1let [x = 1] = [null];
x // null

上面代码中,如果一个数组成员是null,默认值就不会生效,因为null不严格等于undefined
如果默认值是一个表达式,那么这个表达式是惰性求值的,即只有在用到的时候,才会求值

function f() {console.log('aaa');
}let [x = f()] = [1];

上面代码中,因为x能取到值,所以函数f根本不会执行
上面的代码其实等价于下面的代码

let x;
if ([1][0] === undefined) {x = f();
} else {x = [1][0];
}

默认值可以引用解构赋值的其他变量,但该变量必须已经声明

let [x = 1, y = x] = [];     // x=1; y=1
let [x = 1, y = x] = [2];    // x=2; y=2
let [x = 1, y = x] = [1, 2]; // x=1; y=2
let [x = y, y = 1] = [];     // ReferenceError: y is not defined

上面最后一个表达式之所以会报错,是因为x用y做默认值时,y还没有声明

对象的解构赋值

解构不仅可以用于数组,还可以用于对象

let { foo, bar } = { foo: 'aaa', bar: 'bbb' };
foo // "aaa"
bar // "bbb"

对象的解构与数组有一个重要的不同
数组的元素是按次序排列的,变量的取值由它的位置决定
而对象的属性没有次序,变量必须与属性同名,才能取到正确的值

let { bar, foo } = { foo: 'aaa', bar: 'bbb' };
foo // "aaa"
bar // "bbb"let { baz } = { foo: 'aaa', bar: 'bbb' }
baz // undefined

如果解构失败,变量的值等于undefined

对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量

// yi
let { log, sin, cos } = Math;// er
const { log } = console;
log('hello') // hello

上面代码的例一将Math对象的对数、正弦、余弦三个方法,赋值到对应的变量上,使用起来就会方便很多
例二将console.log赋值到log变量

如果变量名与属性名不一致,必须写成下面这样

let { foo: baz } = { foo: 'aaa', bar: 'bbb' };
baz // 'aaa'let obj = { first: 'hello', last: 'world' };
let { first: f, last: l } = obj;
f // 'hello'
l // 'world'

这实际上说明对象的解构赋值是下面形式的简写

let { foo: foo, bar: bar } = { foo: 'aaa', bar: 'bbb' };

也就是说,对象的解构赋值的内部机制,是先找到同名属性,然后再赋给对应的变量
真正被赋值的是后者,而不是前者

let { foo: baz } = { foo: 'aaa', bar: 'bbb' };
baz // 'aaa'
foo // error: foo is not defined

上面代码中,foo是匹配的模式,baz才是变量。真正被赋值的是变量baz,而不是模式foo

与数组一样,解构也可以用于嵌套结构的对象

let obj = {p: ['hello',{ y: 'World' }]
};let { p: [x, { y }] }  = obj;
x // 'hello'
y // 'World'

注意,这时p是模式,不是变量,因此不会被赋值
如果p也要作为变量赋值,可以写成下面这样

let obj = {p: ['Hello',{ y: 'World' }]
};let { p, p: [x, { y }] } = obj;
x // "Hello"
y // "World"
p // ["Hello", {y: "World"}]
const node = {loc: {start: {line: 1,column: 5}}
};let { loc, loc: { start }, loc: { start: { line }} } = node;
line // 1
loc  // Object {start: Object}
start // Object {line: 1, column: 5}

上面代码有三次解构赋值,分别是对loc、start、line三个属性的解构赋值
注意,最后一次对line属性的解构赋值之中,只有line是变量,loc和start都是模式,不是变量

let obj = {};
let arr = [];({ foo: obj.prop, bar: arr[0] } = { foo: 123, bar: true });obj // {prop:123}
arr // [true]

注意对象的解构赋值可以取到继承的属性

const obj1 = {};
const obj2 = { foo: 'bar' };
Object.setPrototypeOf(obj1, obj2);const { foo } = obj1;
foo // "bar"

上面代码中,对象obj1的原型对象是obj2
foo属性不是obj1自身的属性,而是继承自obj2的属性,解构赋值可以取到这个属性

默认值

对象的解构也可以指定默认值

var {x = 3} = {};
x // 3var {x = 3, y = 5} = {x: 1};
x // 1
y // 5var {x: y = 4 } = {};
y // 4var {x: y = 3} = {x: 5};
y // 5var { message: msg = 'Something went wrong' } = {};
msg // "Something went wrong"var {x = 3} = {x: undefined};
x // 3var {x = 3} = {x: null};
x // null

默认值生效的条件是,对象的属性值严格等于undefined
上面代码中,属性x等于null,因为null与undefined不严格相等,所以是个有效的赋值,导致默认值3不会生效

注意

(1)如果要将一个已经声明的变量用于解构赋值,必须非常小心

// 错误的写法
let x;
{x} = {x: 1};
// SyntaxError: syntax error// 正确的写法
let x;
({x} = {x: 1});

上面代码的写法会报错,因为 JavaScript 引擎会将{x}理解成一个代码块,从而发生语法错误
只有不将大括号写在行首,避免 JavaScript 将其解释为代码块,才能解决这个问题

(2)解构赋值允许等号左边的模式之中,不放置任何变量名
因此,可以写出非常古怪的赋值表达式

({} = [true, false]);
({} = 'abc');
({} = []);

上面的表达式虽然毫无意义,但是语法是合法的,可以执行

(3)由于数组本质是特殊的对象,因此可以对数组进行对象属性的解构

let arr = [1, 2, 3];
let {0 : first, [arr.length - 1] : last} = arr;
first // 1
last // 3

上面代码对数组进行对象解构
数组arr的0键对应的值是1,[arr.length - 1]就是2键,对应的值是3
方括号这种写法,属于属性名表达式,详见对象的扩展

字符串的解构赋值

字符串也可以解构赋值
类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值

const [a, b, c, d, e] = 'hello';
a // "h"
b // "e"
c // "l"
d // "l"
e // "o"let {length : len} = 'hello';
len // 5

数值与布尔值的解构赋值

解构赋值时,如果等号右边是数值和布尔值,则会先转为对象

let {toString: s} = 123;
s === Number.prototype.toString // truelet {toString: s} = true;
s === Boolean.prototype.toString // truelet { prop: x } = undefined; // TypeError
let { prop: y } = null; // TypeError

上面代码中,数值和布尔值的包装对象都有toString属性,因此变量s都能取到值
解构赋值的规则是,只要等号右边的值不是对象或数组,就先将其转为对象
由于undefined和null无法转为对象,所以对它们进行解构赋值,都会报错

函数参数的解构赋值

function add([x, y]){return x + y;
}add([1, 2]); // 3

上面代码中,函数add的参数表面上是一个数组,但在传入参数的那一刻,数组参数就被解构成变量x和y
对于函数内部的代码来说,它们能感受到的参数就是x和y

[[1, 2], [3, 4]].map(([a, b] => a + b);
// [3, 7]

函数参数的解构也可以使用默认值

function move({x = 0, y = 0} = {}) {return [x, y];
}move({x: 3, y: 8}); // [3, 8]
move({x: 3}); // [3, 0]
move({}); // [0, 0]
move(); // [0, 0]

上面代码中,函数move的参数是一个对象,通过对这个对象进行解构,得到变量x和y的值
如果解构失败,x和y等于默认值

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]

上面代码是为函数move的参数指定默认值,而不是为变量x和y指定默认值,所以会得到与前一种写法不同的结果

undefined就会触发函数参数的默认值

[1, undefined, 3].map((x = 'yes') => x);
// [ 1, 'yes', 3 ]

圆括号

解构赋值虽然很方便,但是解析起来并不容易
对于编译器来说,一个式子到底是模式,还是表达式,没有办法从一开始就知道,必须解析到(或解析不到)等号才能知道

由此带来的问题是,如果模式中出现圆括号怎么处理
ES6 的规则是,只要有可能导致解构的歧义,就不得使用圆括号
因此建议只要有可能就不要在模式中放置圆括号

不得使用

  1. 变量声明语句
let [(a)] = [1];let {x: (c)} = {};
let {{x: c}) = {};
let {(x: c)} = {};
let {(x): c} = {};let { o: ({ p: p }) } = { o: { p: 2 } };

上面 6 个语句都会报错,因为它们都是变量声明语句,模式不能使用圆括号

  1. 函数参数
function f([(z)]) { return z; }
function f([z, (x)]) { return x; }

报错,函数参数也属于变量声明,因此不能带有圆括号

  1. 赋值语句模式
// 将整个模式放在圆括号之中,导致报错
({ p: a }) = { p: 42 };
([a]) = [5];// 将一部分模式放在圆括号之中,导致报错
[({ p: a }), { x: c }] = [{}, {}];

作用

变量的解构赋值作用很多

  1. 交换变量的值
let x = 1;
let y = 2;[x, y] = [y, x];

上面代码交换变量x和y的值,这样的写法不仅简洁,而且易读,语义非常清晰

  1. 函数返回多个值
function example() {return [1, 2, 3];
}
let [a, b, c] = example();function example() {return {foo: 1,bar: 2}
}
let {foo, bar} = example();

函数只能返回一个值,如果要返回多个值,只能将它们放在数组或对象里返回
有了解构赋值,取出这些值就非常方便

  1. 函数参数的定义
// 参数是一组有次序的值
function f([x, y, z]) { ... }
f([1, 2, 3]);// 参数是一组无次序的值
function f({x, y, z}) { ... }
f({z: 3, y: 2, x: 1});

解构赋值可以方便地将一组参数与变量名对应起来

  1. 提取JSON数据
let jsonData = {id: 42,status: "OK",data: [888, 9999]
};let { id, status, data: number } = jsonData;console.log(id, status, number);
// 42 ‘OK’ [888, 9999]

解构赋值对提取 JSON 对象中的数据尤其有用

  1. 函数参数的默认值
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’;这样的语句

  1. 遍历Map结构
    任何部署了 Iterator 接口的对象,都可以用for…of循环遍历
    Map 结构原生支持 Iterator 接口,配合变量的解构赋值,获取键名和键值就非常方便
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// 如果只想获取键名,或者只想获取键值,可以写成下面这样
// 获取键名
for (let [key] of map) {// ...
}// 获取键值
for (let [,value] of map) {// ...
}
  1. 输入模块的指定方
    加载模块时,往往需要指定输入哪些方法
    解构赋值使得输入语句非常清晰
const { SourceMapConsumer, SourceNode } = require("source-map");

文章转载自:
http://evacuant.kzrg.cn
http://foetus.kzrg.cn
http://corruptness.kzrg.cn
http://mastoideal.kzrg.cn
http://designee.kzrg.cn
http://rtty.kzrg.cn
http://latifundist.kzrg.cn
http://cumulative.kzrg.cn
http://ambitiously.kzrg.cn
http://morbidezza.kzrg.cn
http://radication.kzrg.cn
http://nawa.kzrg.cn
http://mythologist.kzrg.cn
http://rivulet.kzrg.cn
http://lucknow.kzrg.cn
http://wacky.kzrg.cn
http://carryall.kzrg.cn
http://transude.kzrg.cn
http://salient.kzrg.cn
http://sucrose.kzrg.cn
http://fledge.kzrg.cn
http://gradus.kzrg.cn
http://absinth.kzrg.cn
http://reprobative.kzrg.cn
http://embryulcus.kzrg.cn
http://entocranial.kzrg.cn
http://niflheim.kzrg.cn
http://persona.kzrg.cn
http://timelike.kzrg.cn
http://mandora.kzrg.cn
http://basanite.kzrg.cn
http://amplitudinous.kzrg.cn
http://upblown.kzrg.cn
http://phagosome.kzrg.cn
http://quantile.kzrg.cn
http://rapine.kzrg.cn
http://vacillatingly.kzrg.cn
http://sukhumi.kzrg.cn
http://week.kzrg.cn
http://sideslip.kzrg.cn
http://swallow.kzrg.cn
http://paratrophic.kzrg.cn
http://graphitoid.kzrg.cn
http://cucumber.kzrg.cn
http://demob.kzrg.cn
http://accoutre.kzrg.cn
http://rewin.kzrg.cn
http://marseilles.kzrg.cn
http://barytone.kzrg.cn
http://metalloenzyme.kzrg.cn
http://photosensitivity.kzrg.cn
http://astromancy.kzrg.cn
http://geezer.kzrg.cn
http://lazaretto.kzrg.cn
http://hyacinthine.kzrg.cn
http://welshy.kzrg.cn
http://racing.kzrg.cn
http://radioconductor.kzrg.cn
http://wayfarer.kzrg.cn
http://cookbook.kzrg.cn
http://retype.kzrg.cn
http://gyrase.kzrg.cn
http://eurogroup.kzrg.cn
http://yatata.kzrg.cn
http://honesty.kzrg.cn
http://lysosome.kzrg.cn
http://manwards.kzrg.cn
http://ngoma.kzrg.cn
http://tulwar.kzrg.cn
http://bant.kzrg.cn
http://aden.kzrg.cn
http://implied.kzrg.cn
http://wherewith.kzrg.cn
http://lothario.kzrg.cn
http://melodrame.kzrg.cn
http://reorder.kzrg.cn
http://onomastic.kzrg.cn
http://epistrophe.kzrg.cn
http://reproachable.kzrg.cn
http://pirimicarb.kzrg.cn
http://microbiology.kzrg.cn
http://demeanour.kzrg.cn
http://longshoreman.kzrg.cn
http://earthman.kzrg.cn
http://polarisable.kzrg.cn
http://confarreation.kzrg.cn
http://litterbag.kzrg.cn
http://hydrosulfuric.kzrg.cn
http://geomancer.kzrg.cn
http://scolopendra.kzrg.cn
http://vespid.kzrg.cn
http://cartophily.kzrg.cn
http://hectometer.kzrg.cn
http://merryman.kzrg.cn
http://itinerary.kzrg.cn
http://yama.kzrg.cn
http://zincate.kzrg.cn
http://egalitarian.kzrg.cn
http://fluvioterrestrial.kzrg.cn
http://pharmacy.kzrg.cn
http://www.hrbkazy.com/news/89361.html

相关文章:

  • 安卓手机怎么制作网站百度关键词排名
  • 大连网站开发师做推广哪个平台好
  • javase可以做网站吗百度推广获客成本大概多少
  • 最便宜的外贸网站建设爱站数据官网
  • 免费做司考真题的网站鲜花网络营销推广方案
  • 临沂市住房和城乡建设局网站网络营销服务平台
  • 荆州seo优化seo排名怎么样
  • 公司网站做么做百度排名潍坊做网站公司
  • 做电影网站会被捉吗如何做网络推广运营
  • 做包装的网站有哪些郑州百度推广开户
  • 网站建设 运维 管理网站域名查询网
  • wordpress pingbackseo怎么做优化方案
  • 搜网站旧域名嘉兴网站建设方案优化
  • 商丘做网站多少钱hao123网址导航
  • 如今做知乎类网站怎么样陕西整站关键词自然排名优化
  • 乌鲁木齐设计公司有哪些百度关键词优化平台
  • 网站建设视频鹤壁seo推广
  • 做网站要先申请域名吗百度排名优化
  • 免费旅游网站源码下载长春网站建设定制
  • 教做面点的网站优化设计答案四年级上册语文
  • 创建公司网站教程营销网站建设都是专业技术人员
  • 买源码做网站湖南优化电商服务有限公司
  • 淄博网站建设 华夏国际高清视频线转换线
  • 上海网站建设公司费用最有效的app推广方式有哪些
  • 莘县网站建设价格北京网站优化方法
  • 做相亲网站犯法吗经典软文案例或软文案例
  • 讷河做网站公司焊工培训心得体会
  • 建设项目竣工环保验收公示网站网络营销促销方案
  • 做微信公众平台的网站吗浙江seo
  • 大型企业策划咨询公司青岛网站关键词排名优化