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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:08:02+00:00 2026-05-30T02:08:02+00:00

I found something very odd today: If you create objects with a constructor function

  • 0

I found something very odd today: If you create objects with a constructor function and the new keyword, but return a function from the constructor, it behaves like so:

  1. The newly-created “object” is instead a function.
  2. That new function can be invoked like normal, however…
  3. If you maintain a reference to this in the constructor function, this references an object that was correctly created from the constructor. It’s what you expected to be returned from new.

Here’s an example:

function Constructor() {
  var self = this;

  this.name = 'instance';
  return function() {
    return self;
  }
}

So if you instantiated it like this: var instance = new Constructor()
The following would result:

typeof instance    //returns "function"
typeof instance()  //returns "object"
instance()         //returns { name: 'instance' }

So I guess I have three questions:

  1. Is this legal and does it work cross-browser? It’s really awesome and I think it can be used in a lot of ways, but is this behavior dependable?
  2. What happens in the background that causes this behavior?
  3. (maybe answered by 2, but…) Is the new object (the one referenced with ‘this’) inside the new instance, so that it’s all self-contained and is cleaned up properly by the garbage collector?
  • 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-30T02:08:03+00:00Added an answer on May 30, 2026 at 2:08 am
    1. Yes, while a constructor by default returns the new object being constructed (which is referenced by this), you can override that return value as long as you return an object. Because a function is an object, you can return it as you are in your example. The newly created object is not a function itself, but your returned function references the newly created object in its variable scope.

    2. See #1

    3. This is because a function creates a closure, so it continues to reference the self variable, which happens to reference the actual object being constructed. So I wouldn’t quite say it’s “inside” anything, but rather is simply part of the variable scope of the function.

    The thing to understand is that your function isn’t any different from any other function. Just like if you had instead returned an Array, you’d just have a regular Array, which could reference the new object.

    function Constructor() {
    
      this.name = 'instance';
      return [ this ];  // Instead return an Array that references the new object
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning WPF nowadays. Found something new today with .Net dependency properties. What they bring
I am new to opengl programming, but I am doing something very basic, and
So I'm looking at slickdeals.net and amazon.com, and I found something very intriguing. It's
I found something like this in a SqlServer DB stored proc: SELECT stuff FROM
I have found some odd behaviour while I was using the PHP function in_array()
This may seem a bit odd, but I really need to create a workaround
I've just found something very strange in PHP. If I pass in a variable
I found something similar discussed, but it was while .NET 4.0 RC. As for
I feel like I am missing something very simple here, but this is the
i studying TDateTime functions and procedure, but not found something that allow me to

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.