JavaScript是否有类似于php list命令的语言结构?

时间:2015-02-19 21:43:17

标签: javascript php node.js symbol-table language-construct

JavaScript是否有语言结构或类似于php list命令? http://php.net/manual/en/function.list.php

此命令将数组的值分配给单个语句中的变量。例如,给定数组:

$info = array('Coffee', 'brown', 'caffeine');

list命令将每个数组元素值分配给命名变量:

list($drink, $color, $power) = $info;

这样:

   echo "$drink is $color and $power makes it special."; // results in:

   Coffee is brown and caffeine makes it special.

因此,这是一种在一个语句中为多个变量赋值的简单方法。

JavaScript是否具有相同的功能,而不必单独分配每个变量?

有没有办法使用对象的属性或数组的元素?如果没有,可以编写一个可以执行此操作的函数吗?如果执行此操作的唯一方法是通过函数,则函数将需要访问定义变量的范围。

我在这个五年前的问题中尝试了解决方案:Javascript equivalent of PHP's list(),但他们没有工作。 Array原型更改无法在我的node.js环境中分配变量,左手数组赋值是Chrome中的参考错误。有人谈论一种实验性的新技术,但谈话已经有几年了。我想知道是否有一个解决方案比链接问题中列出的更好。

4 个答案:

答案 0 :(得分:2)

这称为"解构分配"在ECMAScript 6中Mozilla Developer Network有一些例子:

var a, b;
[a, b] = [1, 2]
{a, b} = {a:1, b:2}

目前在节点中不支持它,仅在某些浏览器中支持,正如您在ECMAScript compatibility table中看到的那样。如果您使用类似Babel的代码将代码转换为ECMAScript 5,您仍然可以使用它。

答案 1 :(得分:1)

php.js编写的实验函数试图将php的列表函数改编为js,也许你可以建立在这个基础上:

function list() {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // %        note 1: Only works in global context and deviates (by necessity) from
  // %        note 1: PHP version by adding the array (which in PHP is an rvalue
  // %        note 1: separate from the list() lvalue) as the last argument
  // *     example 1: var drink, color, power;
  // *     example 1: list('drink', 'color', 'power', ['coffee', 'brown', 'caffeine']);
  // *     example 1: drink +' is '+color+' and '+power+' makes it special.\n';
  // *     returns 1: 'coffee is brown and caffeine makes it special.\n'

  var i = 0, arr = [];

  arr = arguments[arguments.length - 1];

  if (arr && typeof arr === 'object' && arr.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
    return arr.list.apply(arr, Array.prototype.slice.call(arguments, 0, -1));
  }
  if (arr && typeof arr === 'object' && arr.length && !arr.propertyIsEnumerable('length')) {
    for (i = 0; i < arr.length; i++) {
      this.window[arguments[i]] = arr[i];
    }
  }
  else {
    for (i in arr) {
      if (i.length === parseInt(i).toString().length && parseInt(i) < arguments.length - 1) {
        this.window[arguments[i]] = arr[i];
      }
    }
  }

  return arr;
}

答案 2 :(得分:1)

我刚刚编写了一个简单的函数,它的工作原理。只是不完全像PHP中的列表。这是

function list(fn,array){
   if(fn.length && array.length){
       for(var i=0;i<array.length;i++){
            var applyArray = [];
            for(var j=0;j<array[i].length;j++){
                fn[j] = array[i][j];
                applyArray.push(fn[j]);
            }
            fn.apply(this,applyArray);
       }
   }
}

这并不是什么壮观的事情,但如果你正在为自己寻找一些简单的用例,那就是它。

如何使用?

//array array mixture for composure
var arrayMixture = [ ["coffee","sugar","milk"], ["tea","sugar","honey"] ];
//call our function
list(function(treat,addin,addin2){
   console.log("I like "+treat+" with " + addin + " and " + addin2);
},arrayMixture);
//output:
//I like coffee with sugar and milk
//I like tea with sugar and honey

希望这就是你要找的东西

答案 3 :(得分:0)

我认为,对于没有嵌套数组的情况,可以大大简化answer EasyBB

var info = [ "Coffee", "brown", "caffeine" ];

(function(drink, color, power) {
    console.log(drink + " is " + color + " and " + power + " makes it special.");
}.apply(this, info));

现在您不必定义额外的功能来完成工作。当然,采用ES6会更好,我们可以使用数组解构。与此同时,我可能只会使用老式的var drink = info[0], color = info[1], power = info[2];;它更具可读性,使用的内存更少。

相关问题