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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:36:32+00:00 2026-06-03T08:36:32+00:00

I began to like the following pattern (sry, Coffeescript here cause it’s more readable

  • 0

I began to like the following pattern (sry, Coffeescript here cause it’s more readable in that case):

Parent = (proto)->
  self = Object.create proto
  public1 = ->
    console.log "in public1 with self: ", self
    console.log "in public1 with name: ", self.name

  self.public1 = public1
  self

Child = (proto)->
  self = new Parent proto

  private1 = ->
    console.log "in private1 with self: ", self
    console.log "in private1 with name: ", self.name
    self.public1()

  public2 = ->
    console.log "in public2 with self: ", self
    console.log "in public2 with name: ", self.name
    private1()

  self.public2 = public2
  self

GrandChild = (proto)->
  self = new Child proto

  public3 = ->
    console.log "in public3 with self", self
    console.log "in public3 with name: ", self.name
    self.public2()

  self.public3 = public3
  self

felix = new GrandChild name: "Felix"
felix.public2()

That naive attempt of multiple inheritance works and enables a simple and obviously convenient use of ‘self’ … smart when you’re coming from other oop languages like I do.

Gotcha: every GrandChild object creates a NEW Child as well as a NEW Parent object, so that memory consumption grows in case many GrandChild objects are created.

Augmenting GrandChild’s prototype with Child’s and Parent’s methods would only reference their methods in a GrandChild object as far as I understood (and save lots of space), but reading up and down I do not find a way to have access to self the way I have with the upper solution.

I know Coffeescript itself provides a class based inheritance system on top of JS’s prototypal inheritance. Other libs provide solutions as well. I’d like to simply understand what’s going on to pick the right solution depending on the use case.

What if – for example – I would like to put private1 and public2 of Child into a prototype, so that these functions are referenced instead of copied?

Can someone enlighten me at that point?

  • 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-03T08:36:33+00:00Added an answer on June 3, 2026 at 8:36 am

    Gotcha: every GrandChild object creates a NEW Child as well as a NEW
    Parent object, so that memory consumption grows in case many
    GrandChild objects are created.

    False. What you are doing here, say when you new GrandChild, is that you instantiate a Parent and then adorn it with methods from the Child contructor, and then again from the GrandChild constructor. It’s the same object, from Object.create proto all the way through the grandchild. In that respect, you aren’t littering up your context with dummy object at all.

    Augmenting GrandChild’s prototype with Child’s and Parent’s methods
    would only reference their methods in a GrandChild object as far as I
    understood (and save lots of space), but reading up and down I do not
    find a way to have access to self the way I have with the upper
    solution.

    Closure based private methods and prototypes don’t usually get along. You can have a lot of flexibility when you do it all in constructors like this, but you lose the advantages of using the prototype (like speed from not redefining functions in new scopes all the time).

    I know Coffeescript itself provides a class based inheritance system
    on top of JS’s prototypal inheritance. Other libs provide solutions as
    well. I’d like to simply understand what’s going on to pick the right
    solution depending on the use case.

    It does, and when using coffee script there is no reason not to use it. A lot of thought has gone into it. But again private methods will be tricky. Usually private methods are simply made public but underscore prefixed Foo.prototype._privateMethod to denote their privateness. If that’s not good enough, things start to get funky.

    What if – for example – I would like to put private1 and public2 of
    Child into a prototype, so that these functions are referenced instead
    of copied?

    Public methods can go in the prototype and propogate fine, private not so much. Private is not really a good way to think of things in javascript at all, as it’s not supported like other languages. Instead, any value (including functions) that you have access to is either in your current scope via local variables or closure, or it’s a property on an object that is in your current scope via local variables or closure.


    EDIT:

    Lastly, the pattern you have here would be better served without the new keyword. Since you are creating and returning an arbitrary object, you don’t need the JS engine to create one for you. So your code should work if you simply remove the new entirely from your code here.

    Constructor functions have a quirk. When they return an object they return that object instead of the one created via new. Here you are returning self which you made yourself via Object.create. If a constructor function returns a non-object (string, number, function, null) it will actually return this instead. In coffee script this can be tricky because of implicit returns form functions. And when using coffeescript classes, the constructors never return anything, allowing you to deal with this odd problem.

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

Sidebar

Related Questions

For some reason my Visual Studio 2008 began to show warnings for code like:
Yesterday, I began encountering '999' errors on Yahoo-Pipes. It looks like they throttle requests
I'd like to begin thinking about how I can scale up my algorithms that
I have a LaTeX table that looks like this: \begin{table}[!ht] \centering \small \caption{ \bf{Caption}}
It's been a while that I began with log4j; pretty cool logging framework. I've
I've a table [File] that has the following schema CREATE TABLE [dbo].[File] ( [FileID]
The documentation provides a succinct example, and one that looks like it's missing a
I would like to do something like the following: F_BEGIN F(f1) {some code} F(f2)
When I create something like the following: <mx:DataGrid idmyDataGrid itemEditBegin=myDataGrid_itemEditBeginHandler(event) /> When does the
I have a table that includes the following column: mytable <- data.frame(beta_0 = c(1,2,3)

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.