简体   繁体   English

如何在函数中将变量值作为参数传递

[英]How to pass variable value as param in function

I wasnted to know if it was possible to do something along the lines of:我不想知道是否可以按照以下方式做一些事情:

var test = function(testValue){
    if(testValue == true) doThing1();
    if(testValue == false) doThing2();
}

The context of what this is for, I'm obviously new to JS and am creating a small game similar to an agario-but-simpler and I'm using it for movement.这是用于什么的上下文,我显然是 JS 新手,正在创建一个类似于 agario-but-simer 的小游戏,我将它用于移动。 My ideal setup is:我的理想设置是:

class Player {
            /*
             * @param {int} x - x position
             * @param {int} y - y position
             * @param {int} size - size of object
             * 
             */
            constructor(x, y, size) {
                this.x = x;
                this.y = y;
                this.size = size;
                this.points = 0;
                this.velocity = 1;
                this.color = 'white';
                
            }

            addPoints(amount) {
                points += amount;
            }
 
            changeVelocity(newVelocity) {
                velocity = newVelocity;
            }

            moveLeft = function (canMove) {
                if (canMove) { this.x -= this.velocity; }
            }
            moveRight = function (canMove) {
                if (canMove) { this.x += this.velocity; }
            }
            moveUp = function (canMove) {
                if (canMove) { this.y -= this.velocity; }
            }
            moveDown = function (canMove) {
                if (canMove) { this.y += this.velocity; }
            }
        }

I'm looking to have keyDown 'w' set moveUp to true and then complete the movement per screen refresh where I redraw every screen element.我希望 keyDown 'w' 将 moveUp 设置为true ,然后在每次屏幕刷新时完成移动,我重绘每个屏幕元素。 Then when I keyUp 'w' set moveUp to false .然后当我 keyUp 'w' 设置 moveUp 为false I'm looking mostly for if this style of variable-value-parameter-passing is possible.我主要是在寻找这种可变值参数传递方式是否可行。

*EDIT: I'm looking for the keyUp/keyDown to change the variable's value which in turn ALSO directly changes the param passed to the corresponding function. *编辑:我正在寻找 keyUp/keyDown 来更改变量的值,这反过来也直接更改传递给相应函数的参数。

I don't think you need function parameters, use an object property instead.我认为您不需要函数参数,而是使用对象属性。 The keyup/keydown code can call player.enableMove() and player.disableMove() . keyup/keydown 代码可以调用player.enableMove()player.disableMove()

Also, addPoints() and changeVelocity() need to assign this.xxx .此外, addPoints()changeVelocity()需要分配this.xxx

 class Player { /* * @param {int} x - x position * @param {int} y - y position * @param {int} size - size of object * */ constructor(x, y, size) { this.x = x; this.y = y; this.size = size; this.points = 0; this.velocity = 1; this.color = 'white'; this.canMove = true; } addPoints(amount) { this.points += amount; } changeVelocity(newVelocity) { this.velocity = newVelocity; } enableMove() { this.canMove = true; } disableMove() { this.canMove = false; } moveLeft = function() { if (this.canMove) { this.x -= this.velocity; } } moveRight = function() { if (this.canMove) { this.x += this.velocity; } } moveUp = function() { if (this.canMove) { this.y -= this.velocity; } } moveDown = function() { if (this.canMove) { this.y += this.velocity; } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM