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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:54:45+00:00 2026-05-27T21:54:45+00:00

I have an instance function in javascript and for naming conventions I have other

  • 0

I have an instance function in javascript and for naming conventions I have other instance function added as property of the first instance function object. It’s better illustrated in the following working JavaScript.

var MyModule = (function() { // instance function 
    return function() {
      console.log("ran MyModule");
    }
}());

MyModule.RelatedModule = (function() { //second instance function is a property of first instance function object
    return function() {
        console.log("ran RelatedModule");
    }
}())

var inst1 = new MyModule(), // logs "ran MyModule"
    inst2 = new MyModule.RelatedModule(); // logs "ran RelatedModule"

This works as intended with no errors. What I’d like to do though is to create the function definition for MyModule after I’ve created the MyModule object, can anyone help me achieve this? I’ve illustrated my attempts below.

var MyModule = {}; // create object first, try to set a constructor on it later

MyModule.RelatedModule = (function() { // add properties to the MyModule object
    return function() {
        console.log("ran RelatedModule");
    }
}())

// the following does not work, I'd like to set the `MyModule` constructor instance function and retain the `MyModule.RelatedModule` property
MyModule.constructor = (function() {
    return function() {
      console.log("ran MyModule");
    }
}());

So, how do I retain the properties of an object and change it’s constructor?

  • 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-27T21:54:46+00:00Added an answer on May 27, 2026 at 9:54 pm

    You’re confusing a couple of concepts. In your first example, MyModule doesn’t have a constructor, it is a constructor. That is, it’s a function object that you intend to use with the new operator to create new objects. Those objects will have a constructor property that points back to MyModule (the function that created them). Changing this constructor property won’t have any effect on MyModule; that property is just a pointer back to the function that instantiated the object.

    In other words, you can’t change MyModule‘s constructor. That’s a meaningless statement.

    Now, when you write:

    var MyModule = {};
    

    …you create a new object whose constructor property is Object():

    console.log(MyModule.constructor) // prints Object()
    

    Again, changing this property doesn’t really do much (except obfuscate some useful book-keeping).

    At this point MyModule is just a plain-old object. It’s not a function at all. That’s why you’re getting the “not a function” error. Because it’s not, but you’re trying to use it as though it is. If you want that name to refer to a function (i.e. to a different object) then you’re going to lose all references to any properties you previously set, because you’re pointing at an entirely new object.

    That’s just the way it is.

    Now, you could save a reference to the object that contains all those previously-set properties and copy them back into MyObject once you’ve pointed that name at a function. But I’m not sure what the point would be.

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

Sidebar

Related Questions

stated here https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function the constructor property of an instance of a function object specifies
I have two classes that each need an instance of each other to function.
I have following Javascript objects: function Alfa() { this.status=''; this.setStatus = function(value) { this.status=value
I now have a working JavaScript autocomplete function, thanks to help from many of
So I have the following JavaScript: <script language=JavaScript> function reload(form){ var val=form.profile.options[form.profile.options.selectedIndex].value; self.location='?profile=' +
If you have an instance of an object in javascript, it seems it that
I have a function that accepts a class (not an instance) and, depending on
When you have a function with a string parameter, does that create another instance
Is it possible to have default arguments in MATLAB? For instance, here: function wave(a,
In a RIA Domain service I have added some utility functions. For instance we

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.