user4584963 user4584963 - 5 months ago 25
CoffeeScript Question

Convert coffeescript function to javscript

I am following this railscast but I don't use coffeescript. I am trying to convert the coffeescript to javascript but I'm running into a problem.


jQuery ->
new AvatarCropper()

class AvatarCropper
constructor: ->
aspectRatio: 1
setSelect: [0, 0, 600, 600]
onSelect: @update
onChange: @update

update: (coords) =>
$("#crop_x").val coords.x
$("#crop_y").val coords.y
$("#crop_w").val coords.w
$("#crop_h").val coords.h

js.erb file

$(document).ready(function() {

$('.crop-image').on('click', function () {
aspectRatio: 1,
setSelect: [0, 0, 100, 100],
onSelect: update,
onChange: update

update: (function(_this) {
return function(coords) {
return $('.user').val(coords.h);


I didn't understand why he decided to make a class and thought it would be more complicated to convert the whole thing. The trouble I'm having is the update function. I just plugged his coffee script for the update function into a converter and used the output. This is causing an error saying update is not defined. Where am I going wrong?

Also bonus question: what's the point of him making a class here?



Your syntax looks wrong... : is used when declaring properties in a object constructor.

Correct is:

function update(coords) {
    return $('.user').val(coords.h);

You can declare update with the var statement, also. The difference is cause function declaration with name goes to the top of the block.