1

So I'm wondering if/how I can use a value in a object as a arg in a function ex:

var mousePos = {
    chaos: (-950, 22)
}
console.log(mousePos.chaos) // chaos

mouse.Move(mousePos.chaos) // which would take two args, and then output Invalid number of arguments.
2
  • 2
    Your "chaos" property will have the value 22, and the -950 will be lost. Commented Nov 7, 2017 at 4:39
  • 2
    JS doesn't have tuples, so you need to use an array [-950, 22] Commented Nov 7, 2017 at 4:42

3 Answers 3

2

Yes you can if you use an array and Function.prototype.apply

var mousePos = {
  chaos: [-950, 22]
}

mouse.Move.apply(mouse, mousePos.chaos)

If you are fancy and are using Node or Babel, you can also use spread syntax:

mose.Move(...mousePos.chaos)
Sign up to request clarification or add additional context in comments.

Comments

1

I think you're looking for an array and spread syntax:

var mousePos = {
    chaos: [-950, 22]
};
console.log(mousePos.chaos) // [-950, 22]

mouse.move(...mousePos.chaos) // equivalent to `mouse.move(-950, 22)`

Comments

1

You are looking for a single object with two value (x,y) for mouse position. so manage your object like key/value base array.

var mousePos = { chaos: {x : -950, y:22} };
mouse.move(mousePos.chaos.x,mousePos.chaos.y) // equivalent to 
mouse.move(-950,22)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.