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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:23:17+00:00 2026-06-13T05:23:17+00:00

I have two classes, one depends on another. It is implemented like this: class

  • 0

I have two classes, one depends on another. It is implemented like this:

class myns.ClassA
  constructor(@serviceB): ->

  publicFunctionA: ->
    privateFunctionB.call this

  privateFunctionB = ->
    @serviceB.someFunction()

then I instantiate it in a glue piece:

myns.classA = new myns.ClassA(myns.serviceB)

and use as:

myns.classA.publicFunctionA()

The problem here is I want to access serviceB from privateFunctionB. Is there more appriopriate way of doing this besides using call ?

Or perhaps my entire approach is tainted too much by my Java backgound? What I need are interdependent code modules, some kind of equivalent to singleton services. I know I could use coffeescript class functions and avoid instantation but how to handle injecting serviceB in a clean way then?

  • 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-13T05:23:19+00:00Added an answer on June 13, 2026 at 5:23 am

    Regarding your question about Java idioms:

    There is no notion of public and private in CoffeeScript. Your privateFunctionB is just a normal function declared in the closure of the class. It is in general not a great practice to simulate private functions, because such functions have very different semantics (for example if privateFunctionB was a variable outside your class it would get overwritten, whereas no such risk exists for publicFunctionA).

    Therefore the best is to write both functions as normal class properties (that is what you call public functions. Then your code simplifies to this:

    class myns.ClassA
      constructor(@serviceB): ->
    
      functionA: ->
        @functionB()
    
      functionB: ->
        @serviceB.someFunction()
    

    Another thing you could do for singleton-like things that you can do in CS but not in java is to use global variables. This is often not considered best practice, but it depends on your application (e.g. it’s a big no no if writing library code, but might be ok in an end-user sort of thing, depending on usage).

    Furthermore you could also consider avoiding the whole class thing and just writing it as a plain object, this possibly will be a good idea if there will be no instances and you plan on doing no subclasing etc.

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

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes, one that inherits from the other. The base class is
I have two (unrelated) classes. The first one is Point: typedef std::complex<double> complex_number; class
I have created two viewController classes such that one is superclass of another.i have
I have the following two classes, one inherits from the other Class A{ void
I have two classes; one is a server, another one is a client, and
I have two classes in a model . One is Sdr_Layer class . Other
I have two classes with one-to-many relationship. For example: class User has_many :numbers ...
I have two classes: one base class an one derived class The base class
I have two classes one and two . Both run threads. class Two is

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.