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 8770641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:35:43+00:00 2026-06-13T17:35:43+00:00

im somewhat new to js or coffeescript and i cant figure out what is

  • 0

im somewhat new to js or coffeescript and i cant figure out what is wrong with my script.

class Token

  fetching_token = $.Deferred()
  assigning_token = $.Deferred()

  constructor: ->
    @token = null
    @got_token = $.Deferred()    

  fetch = ->
    fetching_token.resolve({"access_token": '12355'})

  assign_token = (data) =>
    console.log "TOKEN (instance var): " + @token #undefined? 
    @token = data.access_token
    assigning_token.resolve()

  get_token: ->
    fetch()
    $.when(fetching_token).done (data) =>
      assign_token(data)
    $.when(assigning_token).done =>
      @got_token.resolve()
    undefined


t = new Token
t.get_token()

$.when(t.got_token).done ->
  console.log "FETCHED TOKEN: " + t.token #gives null

Im trying to expose following interface on instance of object: token, got_token, get_token.
For some reason the @token in assign_token is undefined. I tried some combinations with fat arrow but couldnt make it to work.
Thank you in advance

  • 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-13T17:35:44+00:00Added an answer on June 13, 2026 at 5:35 pm

    This is a simple (private) function, not a method:

    f = (arg) -> ...
    

    This is a private function that is bound to the class:

    f = (arg) => ...
    

    The => binds the function to whatever @ (AKA this) is when the function is defined. When you say this:

    class C
        f = (arg) => ...
    

    @ is the class C when f is being parsed so f is sort of a private class method.

    The important thing is that when you say this:

    assign_token = (data) =>
      #...
    

    @ inside assign_token will not be an instance of Token, it will actually be Token itself. Your @token is an instance variable on instances of Token so of course it isn’t defined when you don’t have an instance of Token in @.

    You have a couple options:

    1. Make assign_token an instance method. This makes assign_token publicly accessible.
    2. Force the @ issue using call or apply. This keeps assign_token private but more cumbersome to call.

    The first option looks like this:

    assign_token: (data) ->
        # Same stuff you already have
    

    The second on is (mostly) done when you call assign_token:

    assign_token = (data) ->
        # Same stuff you already have
    
    get_token: ->
      fetch()
      $.when(fetching_token).done (data) =>
        assign_token.call(@, data)
      #...
    

    Note the skinny arrow (->) when defining assign_token, that gives you a simple function so that you can set the desired @ when you call it with the function’s call method.

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

Sidebar

Related Questions

Being somewhat new to Clojure I can't seem to figure out how to do
I'm somewhat new to the ASP.NET MVC architecture and I'm trying to sort out
I am somewhat new to using JPA -- I'll put that out there right
I am somewhat new to PHP and am running into an issue. Here is
I'm somewhat new to GTK and very new to Cairo. I've been tasked to
I am somewhat new to android development and development in general. I have a
I'm somewhat new to Git. So I've got the origin/master and origin/upbranch on my
I am somewhat new to ASP.NET and I am confused by the syntax so
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
I'm somewhat new to OOP programming so it's very likely I'm making some stupid

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.