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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:58:12+00:00 2026-05-27T05:58:12+00:00

I have a set of objects and I want to be able to use

  • 0

I have a set of objects and I want to be able to use the same syntax to either

  • build a new object or
  • load an existing object.

I’ve built a “cache” that takes the object and the context that the object was created using and uses this data to see if the object has been previously created. So, for example, if I build theObject = new myObject(param1, param2), the cache would save the object as { obj: theObject, context: { param1: param1, param2: param2 } } and would be loaded again if I give it a context with the same values of param1 and param2.

I thought I was being clever and would be able to build the object like this:

myObject = function(param1, param2) {
  var context = { param1: param1, param2: param2 };
  var cacheHit = this.cache.find(context);

  if (cacheHit) return cacheHit; // <-- Doesn't work :(

  this.param1 = param1;
  this.param2 = param2;
  this.cache.save(this, context);
};

myObject.prototype = { cache: new myCacheObject }; // Shared cache in all instances

Sadly, I’ve learned that the new keyword means that I cannot use a return statement. I’d like for the cache collections to be internalized so that it’s possible to build similar objects without a built-in cache and they would work the same. However, I don’t think that I can do this with the new keyword.

How would I go about accomplishing this?

Update:

It turns out that this approach actually does work and my problem was stemming from inheritance (particularly since this is JavaScript inheritance). When extending this object, I used the following syntax:

myObject = function(param1, param2) {
  if (arguments.length) {
     var context = { param1: param1, param2: param2 };
     var cacheHit = this.cache.find(context);

     if (cacheHit) return cacheHit;

     this.param1 = param1;
     this.param2 = param2;
     this.cache.save(this, context);
   }
};

// Extended from myObject
anotherObject = function(foo, bar) {
  // Call parent constructor
  myObject.call(this, foo, bar);
};

anotherObject.prototype = new myObject;
anotherObject.prototype.constructor = anotherObject;

Since calling the parent constructor can’t force the child to return a value, the child has to explicitly denote this.

anotherObject = function(foo, bar) {
  return myObject.call(this, foo, bar);
};

Hopefully, I can come up with a cleaner way of doing this since, if the child class performs anything in its constructor, I will have to always check if a value was returned.

anotherObject = function(foo, bar) {
  var possibleCacheHit = myObject.call(this, foo, bar);
  if (possibleCacheHit) return possibleCacheHit;

  // Do stuff
};
  • 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-05-27T05:58:13+00:00Added an answer on May 27, 2026 at 5:58 am

    I’ve learned that the new keyword means that I cannot use a return statement.

    Lucky you, that’s not true. Provided that you return a non-null object reference (see the linked answer for the specifics), the new operator will actually let you do what you want.

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

Sidebar

Related Questions

I have a set of objects that I want to conform to an interface,
I have a list of objects which I want to turn into a set.
I have some POCO objects that are set up for use with Entity Framework
I have a set of objects I'd like to do some operations on, in
I have an odd problem with Django. I have a set of objects that
I have several objects set up in Core Data, one of which is Deck
I have set up a Core Data model where I have two objects, say
Say I have a set myset of custom objects that may be equal although
I have a class that serializes a set of objects (using XML serialization) that
I have an API that filters a set of objects based on the ID

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.