Javascript - 如何找到变量的名称而不是它所拥有的值

时间:2016-10-29 06:56:27

标签: javascript

我有这个功能来设置画布上立方体的移动(随机),everyVar是将要使用的变量,a,b,c,d+,-,*,/的函数(对不起,如果它混乱)。有没有办法找到变量的名称而不是它所拥有的值;我有" user.s = a(b(c(d(r, 2), e), w), q);"我需要一种方法将它放在一个重复的函数,以便变量保持不变但信息更新,但如果我把它放在其他地方,因为它是将有不同的变量。我不能设置另一个变量,如myVariable = q,因为它将保存数字而不是变量本身(我需要它说user.y而不是300)。请尽可能简单地回答你,因为我是初学者,你可能会说。

function setMovement(user){
    var everyVar = [user.x, user.y, user.direction, user.distance]

    var q = everyVar[Math.floor(Math.random()*everyVar.length)];
    var w = everyVar[Math.floor(Math.random()*everyVar.length)];
    var e = everyVar[Math.floor(Math.random()*everyVar.length)];
    var r = everyVar[Math.floor(Math.random()*everyVar.length)];

    user.s = a(b(c(d(r, 2), e), w), q);

    a = operators[Math.floor(Math.random()*operators.length)]
    b = operators[Math.floor(Math.random()*operators.length)]
    c = operators[Math.floor(Math.random()*operators.length)]
    d = operators[Math.floor(Math.random()*operators.length)]
}

2 个答案:

答案 0 :(得分:1)

以下是您可以使用的一些代码:



// Define the operators as functions
var operators = [
    function add(x, y) { return x + y },
    function sub(x, y) { return x - y },
    function mul(x, y) { return x * y },
    function div(x, y) { return x / y }
];

// Don't reference the values, but the property names
var everyVar = ['x', 'y', 'direction', 'distance'];

function setMovement(user){
    var q = everyVar[Math.floor(Math.random()*everyVar.length)];
    var w = everyVar[Math.floor(Math.random()*everyVar.length)];
    var e = everyVar[Math.floor(Math.random()*everyVar.length)];
    var r = everyVar[Math.floor(Math.random()*everyVar.length)];

    var a = operators[Math.floor(Math.random()*operators.length)]
    var b = operators[Math.floor(Math.random()*operators.length)]
    var c = operators[Math.floor(Math.random()*operators.length)]
    var d = operators[Math.floor(Math.random()*operators.length)]

    // Make s a function, so the expression is evaluated with the
    // current values of the user object properties:
    user.s = function () {
        return a(b(c(d(user[r], 2), user[e]), user[w]), user[q]);
    };
    // For debugging, you can use this extra property to see
    // which expression the "random" function evaluates:    
    user.s.description = `${a.name}(${b.name}(${c.name}(${d.name}` +
                         `(user.${r}, 2), user.${e}), user.${w}), user.${q})`;
}

// same data
var user = {
    x: 20,
    y: 10,
    direction: 1,
    distance: 5
}
// Define "random" function user.s
setMovement(user);

console.log( 'user.s() evaluates this expression:\n', user.s.description );

console.log( 'result of user.s():', user.s() );

// Change x and y and display result then:
user.x -= 5; 
user.y += 10;
console.log( 'result of user.s():', user.s() );




注意:小心除法:当第二个参数为0时,你会得到一个运行时错误。

答案 1 :(得分:0)

我现在试图摆弄你的代码。我相信你要找的是Object.keys()

function setMovement(user){
    var everyVar = Object.keys(user);

    var q = everyVar[Math.floor(Math.random()*everyVar.length)];
    var w = everyVar[Math.floor(Math.random()*everyVar.length)];
    var e = everyVar[Math.floor(Math.random()*everyVar.length)];
    var r = everyVar[Math.floor(Math.random()*everyVar.length)];

    user.s = a(b(c(d(r, 2), e), w), q);

    a = operators[Math.floor(Math.random()*operators.length)]
    b = operators[Math.floor(Math.random()*operators.length)]
    c = operators[Math.floor(Math.random()*operators.length)]
    d = operators[Math.floor(Math.random()*operators.length)]
}

现在,您将用户对象的所有属性作为数组中的字符串获取,您可以使用常规数组索引语法将它们用于赋值:

var user = { x:10, y:20,direction:1,distance:10};
var everyVar = Object.keys(user);
var x = user[everyVar[1]];

将10分配给变量x