Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
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. 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. Then when I keyUp 'w' set moveUp to 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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't think you need function parameters, use an object property instead. The keyup/keydown code can call player.enableMove() and player.disableMove().

Also, addPoints() and changeVelocity() need to assign 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;
    }
  }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!