admin管理员组文章数量:1026989
In ES6/ES2015 I can use Object destructuring to pull fields from objects passed as function parameters:
function userId({id}) {
return id;
}
var user = {
id: 42,
};
console.log("userId: " + userId(user)); // "userId: 42"
In the above example, is the id
variable set in the userId
function equivalent to setting it via var
, const
, or let
?
So which of the below is the equivalent function:
Var
function userId(user) {
var id = user.id
return id;
}
Let
function userId(user) {
let id = user.id
return id;
}
Const
function userId(user) {
const id = user.id
return id;
}
(Example function from MDN page linked above, jump to "Pulling fields from objects passed as function parameter)
In ES6/ES2015 I can use Object destructuring to pull fields from objects passed as function parameters:
function userId({id}) {
return id;
}
var user = {
id: 42,
};
console.log("userId: " + userId(user)); // "userId: 42"
In the above example, is the id
variable set in the userId
function equivalent to setting it via var
, const
, or let
?
So which of the below is the equivalent function:
Var
function userId(user) {
var id = user.id
return id;
}
Let
function userId(user) {
let id = user.id
return id;
}
Const
function userId(user) {
const id = user.id
return id;
}
(Example function from MDN page linked above, jump to "Pulling fields from objects passed as function parameter)
Share Improve this question asked Dec 22, 2016 at 1:31 Caleb JayCaleb Jay 2,1993 gold badges41 silver badges76 bronze badges 2-
Well you know it's not
const
, because within the function you can assign another value toid
. – nnnnnn Commented Dec 22, 2016 at 1:36 - 1 There's no meaningful difference between let and var in this case, since the scope of function arguments are always the entire function. – PMV Commented Dec 22, 2016 at 1:37
2 Answers
Reset to default 7Declaring variables with destructuring uses the same scope as if you'd declared ordinary variables in the same position. So if you do:
let {id} = {id: 42};
then it's a let
binding, if you do:
var {id} = {id: 42};
then it's a var
binding.
Function parameters are like var
bindings, since they're scoped to the entire function and they're not const
. They're also like let
bindings because they're scoped to the current block, which is the entire function body.
It will be a local variable scoped over the whole function (as all function parameters are), as in your first and second example (which are equivalent: there is no difference between let
and var
when at the top of the function).
In ES6/ES2015 I can use Object destructuring to pull fields from objects passed as function parameters:
function userId({id}) {
return id;
}
var user = {
id: 42,
};
console.log("userId: " + userId(user)); // "userId: 42"
In the above example, is the id
variable set in the userId
function equivalent to setting it via var
, const
, or let
?
So which of the below is the equivalent function:
Var
function userId(user) {
var id = user.id
return id;
}
Let
function userId(user) {
let id = user.id
return id;
}
Const
function userId(user) {
const id = user.id
return id;
}
(Example function from MDN page linked above, jump to "Pulling fields from objects passed as function parameter)
In ES6/ES2015 I can use Object destructuring to pull fields from objects passed as function parameters:
function userId({id}) {
return id;
}
var user = {
id: 42,
};
console.log("userId: " + userId(user)); // "userId: 42"
In the above example, is the id
variable set in the userId
function equivalent to setting it via var
, const
, or let
?
So which of the below is the equivalent function:
Var
function userId(user) {
var id = user.id
return id;
}
Let
function userId(user) {
let id = user.id
return id;
}
Const
function userId(user) {
const id = user.id
return id;
}
(Example function from MDN page linked above, jump to "Pulling fields from objects passed as function parameter)
Share Improve this question asked Dec 22, 2016 at 1:31 Caleb JayCaleb Jay 2,1993 gold badges41 silver badges76 bronze badges 2-
Well you know it's not
const
, because within the function you can assign another value toid
. – nnnnnn Commented Dec 22, 2016 at 1:36 - 1 There's no meaningful difference between let and var in this case, since the scope of function arguments are always the entire function. – PMV Commented Dec 22, 2016 at 1:37
2 Answers
Reset to default 7Declaring variables with destructuring uses the same scope as if you'd declared ordinary variables in the same position. So if you do:
let {id} = {id: 42};
then it's a let
binding, if you do:
var {id} = {id: 42};
then it's a var
binding.
Function parameters are like var
bindings, since they're scoped to the entire function and they're not const
. They're also like let
bindings because they're scoped to the current block, which is the entire function body.
It will be a local variable scoped over the whole function (as all function parameters are), as in your first and second example (which are equivalent: there is no difference between let
and var
when at the top of the function).
本文标签:
版权声明:本文标题:ecmascript 6 - Does a Javascript Object Destructure assignment use const, let, or var? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744997580a2128671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论