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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:07:52+00:00 2026-05-26T22:07:52+00:00

I have a class definition that is part of a class definition. var someObject

  • 0

I have a class definition that is part of a class definition.

var someObject = {
   someClass: function() {
      this.someMethod = function() {
         alert('hello');
      };
   }
}

I have been told that I should use prototype to add methods, as it then only needs to be created once for all instances of the object. The problem is that it seems I need to add the prototyped method after the constructor function is defined, like this…

var someObject = {
   someClass: function() {

   }
}

someObject.someClass.prototype.someMethod = function() {
   alert('hello');
};

Ideally however I would like to define the prototyped methods within the constructor function like this…

var someObject = {
   someClass: function() {
      this.prototype.someMethod = function() {
         alert('hello');
      };
   }
}

This causes an error however stating that prototype is null or not an object. Is there a way to accomplish what I would like, or is this not possible?

  • 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-26T22:07:53+00:00Added an answer on May 26, 2026 at 10:07 pm

    You can make it work by using arguments.callee or – if you don’t overwrite the .prototype property of your constructor function – this.constructor instead of plain this, ie

    var someObject = {
       someClass: function() {
          // this.constructor.prototype should work as well
          arguments.callee.prototype.someMethod = function() {
             alert('hello');
          };
       }
    };
    

    However, putting the function expression back into the constructor defeats the whole purpose of the exercise – it doesn’t matter that you store the reference to the function object in the prototype instead of the instance, you’re still creating a new one on each constructor invocation!

    One possible solution is using an anonymous constructor instead of an object literal, which gets you an additional scope for free:

    var someObject = new (function() {
        function someClass() {}
    
        someClass.prototype.someMethod = function() {
            alert('hello');
        };
    
        this.someClass = someClass;
    });
    

    See Paul’s answer for an equivalent solution using object literals and a wrapper function, which might be more familiar.

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

Sidebar

Related Questions

I have a class definition that looks like this: public class SolrObj { [SolrUniqueKey(id)]
I have a a Java class that gets a form definition from database then
Let's have the following class definition: CThread::CThread () { this->hThread = NULL; this->hThreadId =
Hello I have a pump class that requires using a member variable that is
I just started using Qt and noticed that all the example class definitions have
Say I have a class definition: class CustomClass { int member; }; Why is
Anyone knows if is possible to have partial class definition on C++ ? Something
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}
I have the following class definition: public interface IItem{} public class FirstObject<T, U> :
Let's say I have an object who's class definition looks like: class Command: foo

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.