Does JavaScript have a language construct or something similar to the php list command? http://ift.tt/V0aoqD
This command will assign the values of an array to variables in a single statement. For example, given the array:
$info = array('coffee', 'brown', 'caffeine');
The list command will assign each of the array element values to named variables:
list($drink, $color, $power) = $info;
such that:
echo "$drink is $color and $power makes it special."; // results in:
Coffee is brown and caffeine makes it special.
So this is an easy way to assign values to many variables in one statement.
Does JavaScript have something equivalent where each variable does not have to be assigned separately?
Is there a way to do this using either an object's properties or an array's elements? If not, can a function be written that would do it? If the only way to do this is via a function, then the function would need access to the scope where the variables are defined.
If I call a function with the field names as parameters, it would need to know where the fields were declared so it can assign the values directly.
Aucun commentaire:
Enregistrer un commentaire