prefer-destructuring
需要从数组和/或对象中解构
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
在 JavaScript ES6 中,添加了一种用于从数组索引或对象属性创建变量的新语法,称为 解构。此规则强制使用解构而不是通过成员表达式访问属性。
规则详情
选项
此规则采用两组配置对象。第一个对象参数确定规则适用于哪些类型的解构。
array
和 object
这两个属性可用于单独打开或关闭每种类型的解构要求。默认情况下,两者都是真的。
PFIxXXLRnqoG75aKxYZihJRKmpDkOnMZTtapxMusnPEDvAWiJZpOfxDbjhVtz9m8FWVqxWdr4vnPVB3vdjamJ86q0w6rBwOi+anSWA0AGGlkKz3xBAqfrB9owaoLg7SREMpnTfUn7LBBFVXR/uPqe70g8ixaFF4RVi9ZLTcFYNZsTbTkUdQU3gO5aGUuVzwrgoA7K6ZRDfvBXO06/QRH2w==
OrWefWf/plG35cbhehKMyyiO51Xxdu6CKFEjY5ZzWV1zKS8v4snAujRp3AGrWic1tQIxYI/rQhbhxJ7k2tP0dH6bQGY+pC/G6lHhwdYZPZ0hmjleuSAJ0xovR+BLZNZOYE59cpjTKXWDwqPY4x9nA7Pm+03Q+pHGvxJR+d6XhFyj6Txcaq9zwl+q3/hbiEuYLqO2zqvgqI4i0xZn6tey51HJ5y4ghF7T4xJM59DVaxWAqAn5HHtirwu1BRDxVjBwe9RXGqjbWv3y0/LhIW7Ksd5s53N1b8ex2wg964TScSlfQpCH9jTfetVQs6DTfboMXMd4mSPlBaKeWwIWPiW9jtkbm40a8+vNoLvp6NOD4eI1kTsot18x2pUCv1bNy1bDTozjW8EtqIqVAiyWnW8c98JqNB9pShiZXsFLEJAPgmVQE6nyAPZpF65UgPjK82jt3Ip7a9oSBgi40d+tI+E7dVk8TtfKAPUK1clqMT8PX644QZ1as7S7dB9J3msfMofJU8gdhud9Oil7zWhcP1DYJIJUZa7SLzyr6IwAeLjhsw8OhNxzBdlAK2NVmIN11f5bxGfi2CsRuMlpju5x9lhir1itGuhztHowurStNCj5y01jFVXmIi2qa1mG+qAtWwD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+pzjaUSrJ+USNLcbG1YHQBEl5FWo8Gg7S7zmQCeoewhxDXSemolmLc6dhCReev1cb2K9iwQ5VyD4e+pM+MCr8ECASLGz+yfm+tHeIZkBw/H6deaP+ka/oWMq149SMTUioR6umbFp3LDmVQ3vYKu5hr42wCQgYygUwE6zOdsE95mMoQGsAARJ1du7Qb+6W+dGUNDlCDXwI8Xq1dq/F37RcbZaGaMJMnqV5pVt5yHrZlN/FxpIiLuhbz4nBUcNeoR+7NtQiaBDc+tlOo0MLwHLnMmGqEiwJhuNNxqi/+A+A7VnW/uLJVMoSG4BGUSziXwZxVmaJbtzZTWu2xVQJHab8Rt2eFm12NiSv05qk24frJvesTrlEVQlpN3RmwD8ADZMkYp2g9Bry7iF+tiYPYDP5DVrx/zzz3/nKxHFFH5Nn7gNBErTIeC9l6XzmydDYBprSwRLK0f4yPB7qUnGh/XMbPpyhQbHRNV6nrdOIsc=
IgbHXOREhuUzqrkIHAUBELO1IBTM0/W5rjS4T6KeTRJddVuoa/qPZgBhw0wa2au1
// With `array` enabled
var foo = array[0];
// With `object` enabled
var foo = object.foo;
var foo = object['foo'];
FphvHt5MK/G5o4JKMpo8wFZScNX7kGyQzE3xqEeYlFcVQkbgr+qufrIGgDR/9BRp
// With `array` enabled
var [ foo ] = array;
var foo = array[someIndex];
// With `object` enabled
var { foo } = object;
var foo = object.bar;
let foo;
({ foo } = object);
v2B3Ju/aQOs6pJkRZut8vJ32hPr6bO9S7jgBKIaRjKu8H7eN2HpTRBhc/U8QQDMIxFtNcd7tsgYflMCPfXQUho3OO351CYRoPwmaOddHsTs=
var foo = object.bar;
v2B3Ju/aQOs6pJkRZut8vJ32hPr6bO9S7jgBKIaRjKu8H7eN2HpTRBhc/U8QQDMIdZuXDEMAn5IwVmigdJiesDd4D2FNTSAsy+MYt01FGjk=
var { bar: foo } = object;
v2B3Ju/aQOs6pJkRZut8vJ32hPr6bO9S7jgBKIaRjKu8H7eN2HpTRBhc/U8QQDMI4pS+MQLKn0YrY/d/w5Nd2QcTdVmHipyGo6HZtcuACmafag5yqQVsGMe/Ie5WLz47
class C {
#x;
foo() {
const bar = this.#x; // private identifiers are not allowed in destructuring
}
}
4pNQUoMoBIPg/MJMy0JjbpzWw/orE7++guXtWDtgTEhdhIUEWg9pchDuz6pd9Dcow+e/6ofkbNxLf8y7hz22Un6X5G2vEukYnigeo40xqzV+pga/mIxu4mkz1V6IXQqFParwdQagPGP0YfnI1IWxjA==
{
"rules": {
"prefer-destructuring": ["error", {
"array": true,
"object": true
}, {
"enforceForRenamedProperties": false
}]
}
}
k7pV66eAcwPDxe3VJQdgLflhzYTcgnHMUO7Rmba3vk+8y7/IQ5JJvdLaYvgG+kIHWnpZFX4TMT0uh58KG6f0qdIPtxt5ZbPGa14YHlL89tMGVHpolC52/WYXmVM0cS/6LyxRZAsL/nukkoEXFXxOWFAAJEocc7kampfDi4F9el0uYTC0EDoEwe3GUdTHQTm4bY8/VJFt8DYncxrzOqenkA==
1G3PbkbejilslWMPCD22L6KpDxJ+Gf9Lq9TzP4E2XQ+AaF8bwS5G8RllauBj8VnHJUgwiMdVnt9X7Kb04/TC8Z/Oy7diGzIk6V0WRTNXUXEJSrxPF5raymR80OoCstHu
{
"rules": {
"prefer-destructuring": ["error", {"object": true, "array": false}]
}
}
4pNQUoMoBIPg/MJMy0JjbpzWw/orE7++guXtWDtgTEitc/9oZnWN17b+CBFZENWPqszm2jTUT4ggtWM1lI7aa1cpDB89hc4s/7EiG1S7LbpsgXRT7MIO+UpPW8+AnTdX11SN5jYWNfauK4gGJWxBL/27IpXMfhPQWHKSksvrias=
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": true
}
}, {
"enforceForRenamedProperties": false
}]
}
}
NmvlPWIclZ/X/hL8FM4lUtUfcg30jSMdFYu603Yyj8ztZVc4/mZMKcmoalimSqLWRScdNtZjJiGLkFJ3MBtKG/vCRVYfYii4NfA5ToE05yjpXW0JX9kuNvhQ2OlonrbauTb1FMXbJalxvpsRT1Z66w9buA5TfEFoZxN997zW4j9jLXZLLkGz9HSU7sx9H2mayrHKdU4WFklE5ZtwpuKEz5u09iFlpnr2LuAuIbyr/7NrQFxc2PzBRl/8Aji56DK4HG5VNpCEEd9+xRmWQVsJYanX7qv/0rJLLGEzUn54lK4=
1G3PbkbejilslWMPCD22L4uA1lS4BbEPL6IHjlBp8O0eDWT7Hy0w8GJBWYX5OIOWz66wysNRyugyDWGQYbar/OpUXl/oY8fDbgrPSsx40kmkQg1+izW4Pn/uJGTvMGtt7L/mUCLS9a4rDkxJZjTJ2Q==
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": false
}
}, {
"enforceForRenamedProperties": false
}]
}
}
cpi3QHLN4NK+cWPr7FWwRwRXRFHGEQBMEmvrtRKbVk+0Dmwcq63kpG0gpUk4W247D9u0YUd3Zu4HtCbiH40TqeM+aRywwh4pY5Sr/K7gf8TiGqNY2cYHxrKsk58yksk9
/* eslint prefer-destructuring: ["error", {VariableDeclarator: {object: true}}] */
var {bar: foo} = object;
8qtNaMVsogGe1eWM4bD+gclJ6N2HNMTxaaDGch9Ttizy3mFz02yX+a2ux4ZTM1vOJAX65XRFEKdCwXRrSjbP3/uk36KAyGxh3ihlB/RBb9JvU6I72i13EPC+O0iYbSka
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: true}}] */
[bar] = array;
何时不使用
/5jih+8yBh5ehPEtslqIpnycFSZ29BsUeBelTt6+7xbcw1iQjKurHDOtQJcdWVh5hYpYiW+VzDaA7aY/fepOLmYJACsmnEdbP2nADCie6pen2sANx2rpZvi7ge06/jk0QOggdLONJ6JEHUMu1SXD0Cr/RdQ36m2X//wBpHLdnjNDqHh/UbwN7TDXwXLzV/Pg
5VrEg4K7IHjaNwSH6QYkRH4/ZfJkxDUUHwq80KmmBzxbq4c0ivDxYWXHEpfhNYNhgIT1Nbpw56gFKmktew/r51bVddI87mC5uZ+94zd9PKM=
var foo = array[100];
bOsiQvcEJma8dAx54M9mL/TFqMmfHl1a131sl+GcSvqkeX2UzMvLViihRrgwrMJkRom0Rp/rt0nsZTxvhv4KCnFu0AOOvsUp5m3wuXC4eqjfw/1GgwqQeMNRwUU2d8crxjd1oTQx1oFXGRfwSayaEg==
mqjHkHwl+XKwNtUtSnmj/ySOXB+PI8pwDQCfgzqnFaV+/L9q+awshwDhDHpLb4bQBxxV3Cdy5E3SqqcdDvgyxA==
var $ = require('jquery');
var foo = $('body')[0];
var [bar] = $('body'); // fails with a TypeError