【问题标题】:JavaScript - How to create a variable name [duplicate]JavaScript - 如何创建变量名[重复]
【发布时间】:2012-10-01 23:25:10
【问题描述】:

可能重复:
Javascript dynamic variable name

我将变量从 onClick 事件传递到 JavaScript 函数。总共有四个变量:两个告诉方向,两个告诉速度变化。我希望该函数评估选择了哪个方向(h_v_,用于水平和垂直),然后应用必要的速度(更快或更慢)。

现在,我通过首先评估方向并根据选择的方向调用不同的changeSpeed 函数来成功地做到这一点。

我想做的是结合这些功能。在示例中,$(direction + "speed") 意味着成为h_speedv_speed

JavaScript 有能力做到这一点吗? (真诚的,米格尔)

var h_speed = 10;
var v_speed = 10;

function changeSpeed(speed, direction){
var direction = direction;
    switch (speed)
    {
        case 'slower':
                        $($direction + "speed") = $($direction + "speed")*2;
                        break;
        case 'faster':
                        $($direction + "speed") = $($direction + "speed")/2;
                        break;
    }
}

这是我的工作代码的两个版本:

版本 1

var h_speed = 10;
var v_speed = 10;

function identifyDirection(speed, direction){
    switch (direction)
    {
        case 'vertical':
                        v_changeSpeed(speed);
                        break;
        case 'horizontal':
                        h_changeSpeed(speed);
                        break;
    }
}

function h_changeSpeed(speed){
    switch (speed)
    {
        case 'slower':
                    h_speed = h_speed*2;
                    break;
        case 'faster':
                    h_speed = h_speed/2;
                    break;
    }
}

function v_changeSpeed(speed){
    switch (speed)
{
        case 'slower':
                    v_speed = v_speed*2;
                    break;
        case 'faster':
                    v_speed = v_speed/2;
                    break;
    }
}

版本 2

/**
 * the changeSpeed functions' arguments
 * are placed directly in the function that
 * determines whether horizontal or vertical
 * speed is changing.
 *
 */

function changeSpeed(speed, direction){
    switch (direction)
{
    case 'vertical':
        switch (speed)
        {
            case 'slower':
                v_speed = v_speed*2;
                break;
            case 'faster':
                v_speed = v_speed/2;
                break;
        }
        break;
    case 'horizontal':
        switch (speed)
        {
            case 'slower':
                h_speed = h_speed*2;
                break;
            case 'faster':
                h_speed = h_speed/2;
                break;
        }
        break;
    }
}

【问题讨论】:

  • if (direction == 'vertical') { v_speed *= speed == 'slower' ? 2 : 0.5; } else { h_speed *= speed == 'slower' ? 2 : 0.5; }
  • 谢谢大家!所有这些回复都很棒。 zerkms,你的代码是我迄今为止尝试过的唯一代码,它可以工作。

标签: javascript variables


【解决方案1】:

变量是变量对象的属性。您可以通过名称访问的唯一变量对象是全局变量对象(全局上下文中的this 或浏览器中的window)。所以对于全局变量你可以这样做:

function hSpeed() {...}

function vSpeed(){...}

// Set direction
var direction = 'h';

// Call related function
window[direction + 'Speed']();

但是,您不能在函数执行上下文中这样做(因为 ECMA-262 明确拒绝访问函数执行和变量对象),您需要使“变量”成为您访问相同对象的属性方式(即使用方括号表示法):

var lib = {};
var lib.hSpeed = function(){...};
var lib.vSpeed = function(){...};

// Set direction
var direction = 'h';

// Call related function
lib[direction + 'Speed']();

【讨论】:

    【解决方案2】:

    将 2 个变量放在一个对象中,例如:

    var directions = {
      horizontal: 1,
      vertical: 1
    }
    

    然后你就可以从参数中取出方向并匹配对象的子对象:

    function changeSpeed(speed, direction) {
      //operate on diections[direction]
    }
    

    至于改变速度,你可以用对象中的函数做类似的事情,但在你的情况下,我只是建议使用另一种数据结构,因为逻辑不会改变,只有参数:

    var speedFactor = {
      faster: 2,
      slower: .5
    }
    

    那么你就可以做任何事情了:

    function changeSpeed(speed, direction) {
      directions[direction] = directions[direction] * speedFactor[speed]
    }
    

    【讨论】:

      【解决方案3】:

      当然有更好的方法来做你想做的事,但如果你想拥有同样的东西(注意你不应该使用全局变量,你可以使用函数作用域将它们设为私有,但这是另一个话题)。

      var speed = {
        h: 10,
        v: 10
      };
      
      function changeSpeed(speedChange, direction) {
        switch (speedChange) {
          case 'slower':
            speed[direction] *= 2;      
            break;
          case 'faster':
            speed[direction] /= 2;      
            break;
        }
      }
      

      现在你可以通过调用来改变速度,例如:

      changeSpeed("slower", "h");
      

      并通过 speed.h 或 speed.v 访问该速度

      【讨论】:

        【解决方案4】:

        好的...

        棘手但:

        //Global namespace
        var speeds = {};
        speeds['h_speed'] = 10;
        speeds['v_speed'] = 10;
        
        
        function changeSpeed(speed, direction){
          var dir = direction.substring(0,1);
          var sp = (speed === 'slower') ? 0.5 : 2;
          //Still accessible from inside your function
          speeds[dir + '_speed'] = speeds[dir + '_speed'] * sp;
        }
        

        会做的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-09
          • 1970-01-01
          • 2018-07-24
          • 2019-07-03
          • 1970-01-01
          • 1970-01-01
          • 2019-06-10
          相关资源
          最近更新 更多