Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6827113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:13:41+00:00 2026-05-26T22:13:41+00:00

Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following

  • 0

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 a CoffeeScript code. And here it 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 great, 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 does the ‘()’ brackets appear after return _results2; line? It’s not a big deal: the code does its work great, but being a little bit perfectionist, I want to know, how to eliminate these round brackets.

UPD: Thank you. Now I understand, what is the ‘()’. But still, I have a question: why does it appear?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:13:42+00:00Added an answer on May 26, 2026 at 10:13 pm

    why does it appear?

    The function scopes the temporary variable that CoffeeScript creates to store the value of the list comprehension (here, _results2). CoffeeScript always creates such a function when a loop is a list comprehension.

    The function isn’t strictly necessary, but it makes the compiled JavaScript more 1:1 with the CoffeeScript source (one of CoffeeScript’s goals). For instance,

    arr = (i for i in [1..3])
    

    compiles to

    arr = (function() { ... })();
    

    whereas without the extra function, you would instead have something like

    var _results2 = [];
    for (...) { ... }
    arr = _results2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript Please anyone explain and
Possible Duplicates: What does this mean? (function (x,y)){…}){a,b); in JavaScript What do parentheses surrounding
Possible Duplicate: What does this “(function(){});”, a function inside brackets, mean in javascript ?
Possible Duplicate: What does this C++ code mean? I'm trying to map a C
Possible Duplicate: What does this JavaScript/jQuery syntax mean? I specifically mean when you do
Possible Duplicate: What does !! mean in ruby? what is this function doing? def
Possible Duplicate: What does (function($) {})(jQuery); mean? I've seen a lot of jQuery code
Possible Duplicate: What does the >?= operator mean? I found this code segment ...
Possible Duplicate: What does this JavaScript snippet mean? i was wondering why is the
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.