js snippets

Just some js snippets i’d like to remember.


Conditional parameter

  function add_square(p, color, size) {
    // Defaults values for the square is middle of screen, red, 10x10
    p     = (typeof p === "undefined") ? { x: 0, y: 0} : p;
    color = (typeof color === "undefined") ? "#FF0000" : color;
    size  = (typeof size === "undefined") ? { x: 10, y: 10} : size;
    ctx = sc.getContext("2d");
    ctx.fillStyle = color;
    ctx.fillRect(p.x, p.y, size.x, size.y);
  }

Repeated IDs in elements in a DOM (with jQuery)


That’s it for now! :)

comments powered by Disqus