EzDevInfo.com

coffeescript interview questions

Top coffeescript frequently asked interview questions

Iterate over Object in coffeescript

I have an object (an "associate array", also known as a plain Javascript object):

obj = {}
obj["Foo"] = "Bar"
obj["bar"] = "Foo"

and I need to iterate over it using coffeescript. Now, doing like this:

for elem in obj

does not work because obj.length is 0, which the compile js code uses. In normal Javascript I would just do

for(var key in obj)

but now I'm wondering: how can I do this in coffeescript?


Source: (StackOverflow)

How to run Gulp tasks synchronously/one after the other

in the snippet like this:

gulp.task "coffee", ->
    gulp.src("src/server/**/*.coffee")
        .pipe(coffee {bare: true}).on("error",gutil.log)
        .pipe(gulp.dest "bin")

gulp.task "clean",->
    gulp.src("bin", {read:false})
        .pipe clean
            force:true

gulp.task 'develop',['clean','coffee'], ->
    console.log "run something else"

In develop task I want to run clean and after it's done, run coffee and when that's done, run something else. But I can't figure that out. This piece doesn't work. Please advise.


Source: (StackOverflow)

Advertisements

How does Trello access the user's clipboard?

When you hover over a card in Trello and press Ctrl+C, the URL of this card is copied to the clipboard. How do they do this?

As far as I can tell, there is no Flash movie involved. I've got Flashblock installed, and the Firefox network tab shows no Flash movie loaded. (That's the usual method, for example, by ZeroClipboard.)

How do they achieve this magic?

(Right at this moment I think I had an epiphany: You cannot select text on the page, so I assume they have an invisible element, where they create a text selection via JavaScript code, and Ctrl+C triggers the browser's default behaviour, copying that invisible node's text value.)


Source: (StackOverflow)

How do I define global variables in CoffeeScript?

On Coffeescript.org:

bawbag = (x, y) ->
    z = (x * y)

bawbag(5, 10) 

would compile to:

var bawbag;
bawbag = function(x, y) {
  var z;
  return (z = (x * y));
};
bawbag(5, 10);

compiling via coffee-script under node.js wraps that so:

(function() {
  var bawbag;
  bawbag = function(x, y) {
    var z;
    return (z = (x * y));
  };
  bawbag(5, 10);
}).call(this);

Docs say:

If you'd like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS. The existential operator (covered below), gives you a reliable way to figure out where to add them, if you're targeting both CommonJS and the browser: root = exports ? this

How do I define Global Variables then in CoffeeScript. What does 'attach them as properties on window' mean?


Source: (StackOverflow)

In CoffeeScript how do you append a value to an Array?

What is the prescribed way to append a value to an Array in CoffeeScript? I've checked the PragProg CoffeeScript book but it only discusses creating, slicing and splicing, and iterating, but not appending.


Source: (StackOverflow)

Examples of CoffeeScript in NodeJS?

As a pet project, I am trying to get familiar with NodeJS and CoffeeScript, and am finding it hard to get the ball rolling.

I've found plenty of examples of single-file super-simple apps like in CoffeeScript's examples folder, however, none of those really show what a full application would look like. I've also scrounged Google to no avail.

Do you have any examples of medium-sized, multi-file CoffeeScript/NodeJS apps I could learn from?


Source: (StackOverflow)

How to use package installed locally in node_modules?

How do I use a local version of a module in node.js. For example, in my app, I installed coffee-script:

npm install coffee-script

This installs it in ./node_modules and the coffee command is in ./node_modules/.bin/coffee. Is there a way to run this command when I'm in my project's main folder? I guess I'm looking for something similar to bundle exec in bundler. Basically, I'd like to specify a version of coffee-script that everyone involved with the project should use.

I know I can add the -g flag to install it globally so coffee works fine anywhere, but what if I wanted to have different versions of coffee per project?


Source: (StackOverflow)

CoffeeScript on Windows?

How can I try CoffeeScript on Windows?

The installation instructions are only for *nix: http://jashkenas.github.com/coffee-script/#installation

EDIT:

Since I asked this a while ago, many new answers have appeared. The number ( and quality ) of options for Windows users has been increased a lot. I "accepted" an answer a long time ago, then changed to other ( better ) answers as they came up, but I have now decided to not accept any answer, and let the community ( votes ) show which answers are best. Thanks to everyone for the input.


Source: (StackOverflow)

What does "Splats" mean in the CoffeeScript tutorial?

Looking at this CoffeeScript tutorial : http://jashkenas.github.com/coffee-script/

I don't quite see what the Splats is for. What is this construction? Where does it come from (historically)


Source: (StackOverflow)

Is there any way to not return something using CoffeeScript?

It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?


Source: (StackOverflow)

CoffeeScript, When to use fat arrow (=>) over arrow (->) and vice versa

When building a class in CoffeeScript, should all the instance method be defined using the => ("fat arrow") operator and all the static methods being defined using the -> operator?


Source: (StackOverflow)

Has anyone used Coffeescript for a production application? [closed]

Coffeescript looks pretty cool. Has anyone used it? What are its Pros & Cons?


Source: (StackOverflow)

Where is body in a nodejs http.get response?

I'm reading the docs at http://nodejs.org/docs/v0.4.0/api/http.html#http.request, but for some reason, I can't seem to to actually find the body/data attribute on the returned, finished response object.

coffee> res = http.get({host:'www.somesite.com',path:'/'})

coffee> res.finished
true

coffee> res._hasBody
true

It's finished (http.get does that for you), so it should have some kind of content. But there's no body, no data, and I can't read from it. Where is the body hiding?


Source: (StackOverflow)

coffeescript - How to comment? "/* this */" doesn't work

In what ways can you comment in Coffeescript? The docs say you can use 3 hash symbols to start and close a comment block

###
  comments
  go
  here
###

I've found that I can sometimes use the following two formats

`// backticks allow for straight-javascript, 
 //but the closing backtick can't be on a comment line (I think?)
`

Are there any simpler ways to insert short comments in coffeescript?

Edit: do NOT use this style

Since this is getting a lot of views, I want to emphasize that this

/* comment goes here */

produces a MATH error when the /* is on its own line.
As Trevor pointed out in a comment on the question, this is a regex, NOT a comment !


Source: (StackOverflow)

Easiest way to check if string is null or empty

I've got this code that checks for the empty or null string. It's working in testing.

eitherStringEmpty= (email, password) ->
  emailEmpty = not email? or email is ''
  passwordEmpty = not password? or password is ''
  eitherEmpty = emailEmpty || passwordEmpty         

test1 = eitherStringEmpty "A", "B" # expect false
test2 = eitherStringEmpty "", "b" # expect true
test3 = eitherStringEmpty "", "" # expect true
alert "test1: #{test1} test2: #{test2} test3: #{test3}"

What I'm wondering is if there's a better way than not email? or email is ''. Can I do the equivalent of C# string.IsNullOrEmpty(arg) in CoffeeScript with a single call? I could always define a function for it (like I did) but I'm wondering if there's something in the language that I'm missing.


Source: (StackOverflow)