What is the advantage of wrapping javascript function parameters with objects rather than not wrapping them at all?
what are the advantages of wrapping javascript function parameters with objects over not wrapping them at all?
for example:
function({x,y,z}){ //1:
return x+y+z;
}
function(x,y,z){ //2:
return x+y+z;
}
question:
see a lot of code written in the first way. How does it compare to the second?
is usually mixed, 1 or 2 or 12.
function({x,y,z},s){ //12
return x+y+z+s;
}
this question is the same as, do I want to use classes? But in fact, I can't organize everything as a class (type 1), nor can I organize everything together with separate attributes (type 2). The advantage of
type 1 is that parameters are, in meaning, a whole, whether from the user's point of view or from the definer's point of view. For example, most plug-ins treat options
as a whole.
reduces the number of parameters, and the code is easier to understand
such as
fn (1Jing 2jue 3) / / look confused
and
fn ({name: 1, age: 2, rank: 3}) / / Oh
this is the use of es6 deconstruction, I think it has the following advantages:
1. As mentioned above, there is no need to pay attention to the order of parameters
2. Parameters are easy to expand
3. Deconstructing is efficient, and if you need some attributes of the object, you can pass them directly without manually extracting
ignoring the order of passing parameters
.