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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:48:09+00:00 2026-06-17T07:48:09+00:00

In JavaScript you can compose objects using some kind of extend function. For example

  • 0

In JavaScript you can compose objects using some kind of extend function.

For example I might have an observable class that exposes a set of public methods (get, push, set, increment, get, etc)

In this case the observable also happens to be an EventEmitter so it also exposes a further set of public methods (emit, on, removeListener, etc)

Both of these classes have internal underscore prefixed properties that store the state. The eventemitter uses _events to store event handlers and the observable uses _state and _id to store state and the id.

Now when I create a model using object composition like so

var Model = extend({}, Observable, {
    constructor: function () {
        // Oops, I was supposed to know Observable uses the _state name already
        this._state = { ... }
    },
    someMethod: function () { ... }
})

This causes an issue because Observable already uses the _state internal property and there is now a name clash.

I would consider it ugly to just “have to know” what objects rely on what internal properties for the mixin to safely work.

How do you avoid mixing in two objects which use the same internal property name?

Ideally this would be solved with ES6 private names, but we can’t do that yet and we can’t emulate them without losing performance. Unless you can provide a ES6 name emulation that does not have a large performance penalty I’m not interested in those solutions.

An alternative would be to use closures or bind but then you would be recreating the functions for every instance with is a significant performance penalty. Another alternative would be to namespace internal properties as __eventEmitter_events and __observable_state. That is just ugly and reduces to probability of a namespace clash, it doesn’t remove it.

  • 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-17T07:48:11+00:00Added an answer on June 17, 2026 at 7:48 am

    The trivial solution is “namespaces”

    var state = "Model@v0.1.3~state";
    var Model = extend({}, Observable, {
        constructor: function () {
            // Nice, I used a namespace and don't clash with "Observable@v0.2.3~state"
            this[state] = { ... }
        },
        someMethod: function () { ... }
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can JavaScript classes/objects have constructors? How are they created?
It's pretty well known that overridden objects in Javascript can cause security issues, or
I'm using javascript to generate a high resolution grid for an image that I
using pure javascript can i select all a tags with href=#xyz. And also i
.get() converts a jQuery object to a DOM element that Javascript can use without
Objects in JavaScript can be used as Hashtable (the key must be String) Is
Using .text object property in JavaScript can produce unexpected results in IE8 (blank string
I was under the impression that these days javascript can be used as a
I have small animation using javascript and css. I made one sprite png file,
Javascript can read the list using for loop. e.g [WebMethod] public static List<EmpName> GetData(int

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.