-1

I want to add condition in constructor for angle if(angle>360){ angle=0; } how to do?

PLAYER[i] = {
    color: "#fff",
    x: 220*i,
    y: 270,
    width: 32,
    height: 32,
    angle: 180
};

each time to use such a condition, takes a lot of space.

Rocket Hazmat
  • 204,503
  • 39
  • 283
  • 323
Arti
  • 5,595
  • 11
  • 44
  • 102
  • 2
    There's no constructor function in your question. – T.J. Crowder Nov 08 '12 at 16:36
  • 1
    When you were asking your question, there was a handy box to the right titled **How to Format**. Worth a read. :-) As is the information available from the **[?]** button on the toolbar that was above the question text area. There's also a preview area underneath that's worth checking so you can see how your post will look and deal with any formatting issues, etc., before posting. – T.J. Crowder Nov 08 '12 at 16:37

1 Answers1

7

This isn't a constructor -- just initialization of an object. But nevermind that. Use the ternary operator: angle: (angle > 360 ? 0 : angle)

Eli Gassert
  • 9,338
  • 3
  • 24
  • 37
  • +1, I think you got to the heart of it. – T.J. Crowder Nov 08 '12 at 16:37
  • 2
    Or just `+(angle <= 360) && angle`, if one has some ternaryfobia. ) – raina77ow Nov 08 '12 at 16:40
  • 2
    Interesting syntax. Haven't seen anyone do it like that before! Was that a "for fun" solution or do people really do that in order to avoid a ternary operation? That seems much less readable to me, but I'm curious if people are actually using it. – Eli Gassert Nov 08 '12 at 16:45