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

  • Home
  • SEARCH
  • 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 7980161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:58:55+00:00 2026-06-04T09:58:55+00:00

CoffeeScript newbie here. I’m having a weird problem with the scope of a function

  • 0

CoffeeScript newbie here. I’m having a weird problem with the scope of a function that I’ve pushed onto an array and then executed within the class’s member function. Basically, it seems like this isn’t getting set correctly.

class TestClass
    constructor: ->
        @functions = [] # my array of functions
        @member = "hello there! come find me!"
    update: =>
        func() for func in @functions

testClass = new TestClass
testClass.functions.push( -> 
    str = "could i find @member? " + @member? + "; this's keys: " + Object.keys(this)
    console.log(str)
)
testClass.update()

The result? Strangely enough, it’s:

could i find @member? false; this's keys: 
top,location,window,external,chrome,v8Locale,document,$,jQuery,CoffeeScript

It seems like the context in which the function is being called is wrong. I thought that by pushing a skinny-arrow function onto my array, when that function is called it would adopt the context in which it is called (update, where this is the testClass)
Everything works just fine if I do:

    update: =>
        func.call(this) for func in @functions

but that doesn’t seem very CoffeeScript idiomatic.

  • 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-06-04T09:58:58+00:00Added an answer on June 4, 2026 at 9:58 am

    By creating your function with the skinny arrow you are creating an anonymous function where this which will be bound to the window object not the context in which it’s called. this does not maintain it’s value across function calls unless both the functions happen to be bound to the same context.

    There’s a few ways I can think to solve this problem and although none of them are particularly ugly, none of them are particularly elegant either.

    First, is the solution you proposed where you bind the function inside the update function. Second is similar, but moves the binding elsewhere:

    class TestClass
        ...
        addFunc: (func) ->
            @functions.push( => func.call( @ ) )
    
    testClass.addFunc( -> ... )
    

    Third option would be to use something like the bind function from underscore.js to bind the function to TestClass at the point it’s created. Though that may end up being messier than the other 2 options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CoffeeScript comes with a few helper functions . How to use them? flatten(Array) for
This is my first Coffeescript function and can't figure out how to get this
Here is my CoffeeScript: jQuery -> $(form).submit (e) -> e.preventDefault() email = $(#email).val() return
I am a newbie in Coffeescript and would like to use the delegate() method
CoffeeScript gets converted to JavaScript, Groovy to Java. Is there a language that was
Here's my CoffeeScript code: $(document).ready -> SearchView = Backbone.View.extend tagName: form className: search events:
I am using CoffeeScript along with the JS splice function. My understanding of the
I really like Coffeescript, but one thing that drives me crazy lately is type
The code below is compiled from CoffeeScript, hope it's still readable (function() { $(function()
The CoffeeScript docs state that list comprehensions should be able to do the select/filter

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.