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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:27:26+00:00 2026-06-10T13:27:26+00:00

So I have a class lets call it A. For this class I have

  • 0

So I have a class lets call it A. For this class I have written a few functions which I can call like this:

var a = new A();
a.getSomething();
a.putSomething();
a.delSomething();

And so on. Now I thought I’d organize it a bit So it wouldn’t get too cluttered and would look like a bit more like this:

a.something.get();
a.something.put();
a.something.del();

And this is how I tried to achieve this:

A.prototype.something = {
  get: function(){...},
  put: function(){...},
  del: function(){...}
};

But these functions (get, put and del) still need to access the common objects/functions found in A, so I need a reference to A, but I don’t know how this can be achieved.

One option I found goes like that:

A.prototype.something = function(){
  var that = this;
  return {
    get: function(){...},
    ...
  };
}; 

And ‘that’ would be used in those (get, put and del) functions instead of ‘this’. But this would mean I would have to call these functions in such way:

a.something().get();
...

Which doesn’t seem very nice to me. So is there a way I could organize these things the way I originally planned?

  • 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-10T13:27:28+00:00Added an answer on June 10, 2026 at 1:27 pm

    So I have a class

    Javascript doesn’t have classes. It has prototype inheritance that can emulate classes to a limited extent, but that isn’t worthwhile purely for the sake of emulating classes. Much better to make optimal use of built–in language features rather than trying to make javascript emulate some other language.

    So you have a constructor…

    I have written a few functions which I can call like this:

    var a = new A();
    a.getSomething();
    a.putSomething();
    a.delSomething();
    

    Presumably those methods are all on A.prototype.

    And so on. Now I thought I’d organize it a bit So it wouldn’t get too cluttered and would look like a bit more like this:

    a.something.get();
    a.something.put();
    a.something.del();
    

    That isn’t less cluttered (to me). I guess there is some common thing that is done by something, and that its get, put, etc. methods want to operate on a not on something.

    The value of this is set by the call, there is no other way to set its value other than with ES5 bind. So the method being called must have access to a somehow. Other answers show how to do that with a closure, but the consequence is that each instance must have its own something object and attached methods.

    The following is similar, but gets rid of the closure and puts the methods on Something.prototype for a bit of efficiency:

    function A(name) {
        this.name = name;
        this.something = new Something(this);
    }
    
    
    function Something(that){
        this.that = that;
    }
    
    Something.prototype.get = function() {
        return this.that.name;
    }
    Something.prototype.put = function(prop, value) {
        this.that[prop] = value;
    }
    
    var a = new A('a');
    
    alert(a.something.get());     // a
    a.something.put('name', 'z');
    alert(a.something.get());     // z
    

    So you can have multiple somethings, each with different put, get, etc. methods. But the intervening something object is really just a device that uses more memory (probably a tiny amount) and requires an extra character. Simpler to keep the something methods on A.prototype and not have to type the extra dot (.).

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

Sidebar

Related Questions

I have a useful object written in PHP lets call it my_class.php <?php class
I have an ActiveRecord class - lets call it comments - which has an
In Visual Studio 2008 (C++) I have a class destructor (Let's call this class
I have a class annotated with @Configuration (let's call it StubConfiguration ) which has
I have a UIImageView based class (lets call it classA) that uses a classB
I have a class, lets call it Book with a property called Name. With
foreword: I have a component, lets call it IView. This component is implemented by
I have written a XS based Perl module which provides access to functions in
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have a class, lets call it Fruit, and I have a HashMap. I

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.