双语学习解构赋值、数组解构和对象解构

双语学习解构赋值、数组解构和对象解构

解构赋值语法是一种 Javascript 表达式。通过解构赋值, 可以将属性值从对象或数组中取出,赋值给其他变量。

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

描述

对象和数组逐个对应表达式,或称对象字面量和数组字面量,提供了一种简单的定义一个特定的数据组的方法。

Description

The object and array literal expressions provide an easy way to create ad hoc packages of data.

数组解构

Array destructuring

为了理解数组解构,我举个例子吧,这个例子是我们家孩子暑假的学习list。他暑假要学习语文、数学、英语,他自己还喜欢画画。

const hugetodoListToday = ['Language', 'math','English','painting']; 
const [Language, math] = hugetodoListToday; 
console.log(Language,math);//Language math

获取到相应位置的值
Get the value to the corresponding location
可以用不同的变量代替数组中第一、第二项的值。例如,用a1、a2代替Langugae和math.

const hugetodoListToday = ['Language', 'math','English','painting']; 
const [a1,a2] = hugetodoListToday; 
console.log(a1,a2);、//Language math

忽略某些返回值

Ignoring some returned values

可以用中间添加逗号的方式忽略返回值,下面的例子,我们忽略了第二项--math,

const hugetodoListToday = ['Language', 'math','English','painting']; 
const [a1,,a2] = hugetodoListToday; 
console.log(a1,a2);//Language English

剩余参数

rest parameter

将剩余数组赋值给一个变量

Assigning the rest of an array to a variable

const hugetodoListToday = ['Language', 'math','English','painting']; 
const [a1,...a2] = hugetodoListToday;
console.log(a1,a2);//Language 0: "math"1: "English"2: "painting"

得到的结果是:一个Language和由其他参数组成的数组

这里补充一点剩余参数的知识:

剩余参数语法允许我们将一个不定数量的参数表示为一个数组。

The rest parameter syntax allows us to represent an indefinite number of arguments as an array.

Description

A function's last parameter can be prefixed with ... which will cause all remaining (user supplied) arguments to be placed within a "standard" javascript array. Only the last parameter can be a "rest parameter".

如果函数的最后一个命名参数以...为前缀,则它将成为一个由剩余参数组成的真数组,其中从0(包括)到theArgs.length(排除)的元素由传递给函数的实际参数提供。

function myFun(a, b, ...manyMoreArgs) { 
console.log("a", a); 
console.log("b", b); 
console.log("manyMoreArgs", manyMoreArgs);
 } 
myFun("one", "two", "three", "four", "five", "six"); 
// Console Output: 
// a, one 
// b, two 
// manyMoreArgs, [three, four, five, six]

补充知识2:实参和形参

function sum(sum1,sum2){//形参
 var c = sum1+sum2; 
for(var i=0;i<arguments.length;++){ 
console.log(arguments[i]); 
}
 console.log(c); 
};
 sum(1,20); //实参

这里的sum1和sum2就是形参

当你调用函数的时候传入的参数就为实参

剩余参数和 arguments对象之间的区别主要有三个:

  • 剩余参数只包含那些没有对应形参的实参,而 arguments 对象包含了传给函数的所有实参。
  • arguments对象不是一个真正的数组,而剩余参数是真正的 Array实例,也就是说你能够在它上面直接使用所有的数组方法,比如 sort,map,forEach或pop。
  • arguments对象还有一些附加的属性 (如callee属性)。

Difference between rest parameters and the arguments object

There are three main differences between rest parameters and the arguments object:

  • rest parameters are only the ones that haven't been given a separate name (i.e. formally defined in function expression), while the arguments object contains all arguments passed to the function;
  • the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly;
  • the arguments object has additional functionality specific to itself (like the calleeproperty).

补充小知识3:

the rest 和the other的区别

the other 一般是指两者中的一个,比如说the one,the other one,或者用来表示另一部分,比如说 一些人同意这个计划,另一些人不同意,就可以说,some people agreed this plan,but the others didn't.

the rest 一般指剩下的全部,例如 the rest of your life,the rest of the money.the rest 一般情况下与of 搭配.

回到正题。

解构数组默认值问题,

Default values

const hugetodoListToday = ['Language', 'math','English','painting']; 
const [a1,a2,a3,a4,a5 = 'program'] = hugetodoListToday;
console.log(a1,a2,a4,a5,a3);
//Language math painting program English

注意输出顺序改了一下哟

如果默认值是null

const hugetodoListToday = ['Language', 'math','English','painting',null]; 
const [a1,a2,a3,a4,a5 = 'program'] = hugetodoListToday; 
console.log(a1,a2,a4,a5,a3);
// Language math painting null English

变量先声明后赋值时的解构

应用场景:交换数组的值,
就是交换变量
Swapping variables

let one = 1, two = 2; 
[one,two] = [two,one];
console.log(one,two);

对象解构
Object destructuring

基本赋值
Basic assignment

const huge = { 
name: 'huge',
 age: 9, 
todoList:{ 
Language: 'Copybook', 
math: 'Paper', 
English: 'word'
 }, 
}
 const {name, age} = huge;
 console.log(name);// huge
 console.log(age);// 9

无声明赋值
Assignment without declaration

let a, b;
 ({a, b} = {a: 1, b: 2});

解构嵌套对象和数组
Nested object and array destructuring

const huge = { 
name: 'huge', 
age: 9,
 todoList:{ 
Language: 'Copybook', 
math: 'Paper', 
English: 'word' 
}, 
} 
const {Language: l, math: m, English: e, painting = 'comic'} = huge.todoList;
console.log(l);//Copybook
 console.log(m);//Paper
 console.log(e); //word
console.log(painting);//comic

上面的例子还给变量重新命名,Language重命名为l。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271