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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:58:00+00:00 2026-06-13T03:58:00+00:00

I would like to nest a number of functions inside a class property as

  • 0

I would like to nest a number of functions inside a class property as shown below.
Unfortunately, they won’t get access to the main scope of the class.

Can I solve this without passing each nested function a reference to this?

class myClass

  constructor: -> @errors = []

  doSomething: -> @errors.push "I work as expected"

  functions:
    doStuff: ->
      @errors.push "I cant access @errors" # => TypeError: Cannot call method 'push' of undefined

    ugly: (context) ->
      context.errors.push "It works, but I am ugly" # Works fine but requires scope injection

Non-working alternative using suggested fat arrow:

class myClass
  constructor: ->
    @errors = []

    @functions:
      doStuff: =>
        @errors.push "I wont work either" # TypeError: Cannot call method 'toString' of undefined

Optional alternative, which doesn’t write to the global this.errors property:

class myClass

  constructor: ->

    @functions =
      errors: []
      doStuff: ->
        @errors.push "I will write to functions.errors only"
  • 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-13T03:58:01+00:00Added an answer on June 13, 2026 at 3:58 am

    In JavaScript (as the result, CoffeeScript too), methods use this of the object that contains method.

    method()                  // this == globalObject
    object.method()           // this == object
    Math.random()             // this == Math
    

    This usually works well, unless you deal with example like yours:

    object.functions.method() // this == object.functions
    

    When dealing with JavaScript, I would avoid having namespace for functions – it doesn’t play well, even with workarounds. For example, you could try putting reference to this object in object.functions, so any function in object.functions would have access to it.

    class MyClass
      constructor: ->
        @errors = []
        @functions.self = this
    
      doSomething: ->
        @errors.push "I work as expected"
    
      functions:
        alsoDoSomething: ->
          @self.errors.push "Also works!"
    

    This appears to work at first, but could be confusing when you are using properties like apply or call on it, obj1.functions.alsoDoSomething.call(obj2) won’t work as the obj2 is not correct object (user should do obj2.functions instead which can be confusing).

    The real solution is: don’t. JavaScript isn’t intended for abuse like this. All object methods should be directly in object prototype. If you have object in it, all methods of it aren’t methods of your object.

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

Sidebar

Related Questions

Is nesting overloaded operators possible ? I would like to nest << inside a
I would like to nest two uiScrollViews, I want to put a uiImageView into
Out of curiosity i would like to know how to best implement a class
I found this ad rotator script and I want to nest JavaScript inside like
I would like to rollback transaction not inside EJB but inside JSF managed bean.
I am trying to nest a UITabBar inside another one, like so: UITabBarController *tabController
I would like to know if it is possible to nest/tunnel javascript rendered sites.
I would like to nest multiple setPropertyActionListener 's in my commandLink but only one
is it possible to nest 2 virtualenvs? I would like to have a base
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode

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.