[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/Destructuring [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 ..

JavaScript , .

In this article

js
var a, b, rest;
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2

[a, b, ...rest] = [1, 2, 3, 4, 5];
console.log(a); // 1
console.log(b); // 2
console.log(rest); // [3, 4, 5]

({ a, b } = { a: 1, b: 2 });
console.log(a); // 1
console.log(b); // 2

({ a, b, ...rest } = { a: 1, b: 2, c: 3, d: 4 });
console.log(a); // 1
console.log(b); // 2
console.log(rest); // { c:3, d:4 }

. . .

, .

, Perl Python.

js
var foo = ["one", "two", "three"];

//  
var one = foo[0];
var two = foo[1];
var three = foo[2];

//  
var [one, two, three] = foo;

, b 1, a 3. , ( - XOR-).

js
var a = 1;
var b = 3;

[a, b] = [b, a];

, . , :

js
function f() {
  return [1, 2];
}

, , . . , f() [1, 2] :

js
var a, b;
[a, b] = f();
console.log("A is " + a + " B is " + b);

[a, b] = f() : a 1, b 2.

:

js
var a = f();
console.log("A is " + a);

a 1 2.

:

js
function f() {
  return [1, 2, 3];
}

var [a, , b] = f();
console.log("A is " + a + " B is " + b);

, a 1, b 3. 2 . ( ) . :

js
[, ,] = f();

exec() , , , . , :

js
var url = "https://developer.mozilla.org/en-US/Web/JavaScript";

var parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
var [, protocol, fullhost, fullpath] = parsedURL;

console.log(protocol); //  "https"

js
var o = { p: 42, q: true };
var { p, q } = o;

console.log(p); // 42
console.log(q); // true

//   
var { p: foo, q: bar } = o;

console.log(foo); // 42
console.log(bar); // true

, Add-on SDK:

js
const { Loader, main } = require("toolkit/loader");

js
var metadata = {
  title: "Scratchpad",
  translations: [
    {
      locale: "de",
      localization_tags: [],
      last_edit: "2014-04-14T08:43:37",
      url: "/de/docs/Tools/Scratchpad",
      title: "JavaScript-Umgebung",
    },
  ],
  url: "/ru/docs/Tools/Scratchpad",
};

var {
  title: englishTitle,
  translations: [{ title: localeTitle }],
} = metadata;

console.log(englishTitle); // "Scratchpad"
console.log(localeTitle); // "JavaScript-Umgebung"

js
var people = [
  {
    name: "Mike Smith",
    family: {
      mother: "Jane Smith",
      father: "Harry Smith",
      sister: "Samantha Smith",
    },
    age: 35,
  },
  {
    name: "Tom Jones",
    family: {
      mother: "Norah Jones",
      father: "Richard Jones",
      brother: "Howard Jones",
    },
    age: 25,
  },
];

for (var {
  name: n,
  family: { father: f },
} of people) {
  console.log("Name: " + n + ", Father: " + f);
}

// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"

-

js
function userId({ id }) {
  return id;
}

function whois({ displayName, fullName: { firstName: name } }) {
  console.log(displayName + " is " + name);
}

var user = {
  id: 42,
  displayName: "jdoe",
  fullName: {
    firstName: "John",
    lastName: "Doe",
  },
};

console.log("userId: " + userId(user)); // "userId: 42"
whois(user); // "jdoe is John"

id, displayName firstName .

, , , :

js
let key = "z";
let { [key]: foo } = { z: "bar" };

console.log(foo); // "bar"

Specification
ECMAScript 2027 LanguageSpecification
# sec-destructuring-assignment
ECMAScript 2027 LanguageSpecification
# sec-destructuring-binding-patterns


Web Proxy Viewer  |  New URL  |  Original Page