Possible duplicate:
What does this mean? (function (x, y)) {...}) {a, b); in javascript
I have the following JS code from the Canvas Documentation :
for(var i=0;i<4;i++){ for(var j=0;j<3;j++){ ctx.beginPath(); var x = 25+j*50; // x coordinate var y = 25+i*50; // y coordinate var radius = 20; // Arc radius var startAngle = 0; // Starting point on circle var endAngle = Math.PI+(Math.PI*j)/2; // End point on circle var anticlockwise = i%2==0 ? false : true; // clockwise or anticlockwise ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise); if (i>1){ ctx.fill(); } else { ctx.stroke(); } } }
I want to turn it into CoffeeScript code. And here he is:
@draw = -> canvas = document.getElementById('canvas') ctx = canvas.getContext('2d') for i in [0..3] for j in [0..2] ctx.beginPath() x = 25 + j * 50 y = 25 + i * 50 radius = 20 startAngle = 0 endAngle = Math.PI + (Math.PI * j) / 2 anticlockwise = if i % 2 == 0 then false else true ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise) if i > 1 then ctx.fill() else ctx.stroke()
Everything works just fine, but I have a question regarding the compiled code:
this.draw = function() { var anticlockwise, canvas, ctx, endAngle, i, j, radius, startAngle, x, y, _results; canvas = document.getElementById('canvas'); ctx = canvas.getContext('2d'); _results = []; for (i = 0; i <= 3; i++) { _results.push((function() { var _results2; _results2 = []; for (j = 0; j <= 2; j++) { ctx.beginPath(); x = 25 + j * 50; y = 25 + i * 50; radius = 20; startAngle = 0; endAngle = Math.PI + (Math.PI * j) / 2; anticlockwise = i % 2 === 0 ? false : true; ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise); if (i > 1) { _results2.push(ctx.fill()); } else { _results2.push(ctx.stroke()); } } return _results2; })()); } return _results; };
So why the brackets '()' appear after the line return _results2; ? It doesn't matter: the code works fine, but as a little perfectionist I want to know how to remove these parentheses.
UPD: Thanks. Now I understand what '()' is. But still I have a question: why does he appear?