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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:56:23+00:00 2026-06-09T02:56:23+00:00

This question deals with CoffeeScript but the same issues still apply to JavaScript. Consider

  • 0

This question deals with CoffeeScript but the same issues still apply to JavaScript. Consider this example:

class Parent    
    func: ->
        alert @member

class Child extends Parent    
    constructor: ->
        @greet()

    greet: ->
        @member = 'hello!'
        @func()

Parent is useless on its own. (new Parent).func() returns undefined.

My questions are:

  • Is there anything wrong with defining classes like Parent which just provide common functions and attributes to child classes? Parent could be a complex class in its own file, providing functionality to classes far down in the prototype chain. Without documentation this could be confusing.

  • In Child.greet, we dynamically add member to a Child object. Is there anything wrong with adding new members to objects without first initializing them in the constructor? Is it better to do constructor: -> @member = null; @greet()? That way, the next programmer knowns what members the object will have and will avoid issues with undefined variables. The flip side is the code will have more noise which JavaScript doesn’t necessitate.

  • Finally, is there a source code which is a good example of idiomatic OOP in CoffeeScript?

  • 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-09T02:56:24+00:00Added an answer on June 9, 2026 at 2:56 am

    Regarding your first question, no, i don’t think there’s anything inherently wrong with doing that. I’d say it’s a case of the template method pattern (using a property access instead of a method, but basically the same). I does add some complexity to your code structure, so, in case it can be resolved in a simpler way, i’d recommend going with the simpler solution.

    About not initializing member variables in the constructor, well, ideally the constructor should leave the object in a usable state, that doesn’t necessarily mean initializing every possible member variable. For that particular code snippet, adding @member = null adds nothing in my opinion. An example of a bad constructor would be one that requires the user to do more stuff than instantiating the object before using it, something like:

    c = new Circle
    c.radius = 5 # Need to set the radius to get the area.
    console.log c.area() # If i hadn't set the radius this would break.
    

    I don’t know about good sources for idiomatic OO CoffeeScript. I think Smooth CoffeeScript is a good book to learn the language, it has a chapter about OO, but i don’t if it is thorough enough.

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

Sidebar

Related Questions

I know this SO question, but it deals with the subject in more general
This is may not be a true programming question but deals with geolocation Api,
This question deals with concurrency issues, for suggestions on how to display a busy
The other answers to this question deal with timing whereas with this problem deals
This question deals with optional arguments passed to a Ruby block. I'm wondering if
This question deals with teamwork and emails. I'm facing the following scenario. I have
My question deals with this UI sample. Having trouble with approach to managing the
This question relates to Java collections - specifically Hashtable and Vector - but may
Let's pretend this is happening inside of a class method (pure example): public function
NOTE: This question deals with an issue observed back in 2011 with an old

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.