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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:35:47+00:00 2026-06-08T05:35:47+00:00

Possible Duplicate: How to detect if a function is called as constructor? I would

  • 0

Possible Duplicate:
How to detect if a function is called as constructor?

I would like to have a function in JavaScript which behaves differently if it’s called normally or if it’s invoked in order to create a new instance. There could be many different uses, where the two functionalities can be related or not related. One use would be to exempt the coder from having to type new, when the function is always used to create new instances:

function MyClass(arg1, arg2) {
  if(/* not called with "new" */) return new MyClass(arg1, arg2);

  // instance initialization stuff here...

}

This way I wouldn’t have to type var x = new MyClass(arg1, arg2): it would be enough to type var x = MyClass(arg1, arg2) (but the former wouldn’t be wrong either).

Just one example of a possible use.

Can this be done? If so, how?

  • 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-08T05:35:52+00:00Added an answer on June 8, 2026 at 5:35 am

    There is no way to be absolutely sure how the function was called, but you can be pretty sure using this instanceof MyClass, like so:

    if (this instanceof MyClass) {
        // Was called with `new`
    } else {
        // Was called as regular function
    }
    

    There are edge cases though, e.g. I could create a new instance of MyClass and then do MyClass.call(new MyClass) — this would create a false positive, as the outer MyClass was not called with new.

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

Sidebar

Related Questions

Possible Duplicate: How to detect if JavaScript is disabled? I have a website which
Possible Duplicate: jquery trigger - ‘change’ function I have a radio button set called
Possible Duplicate: How to detect true Windows version I have an application which uses
Possible Duplicate: Detect Antivirus on Windows using C# In .net I would like to
Possible Duplicate: How to detect which row [ tr ] is clicked? I have
Possible Duplicate: php: determine where function was called from I have an autoloading function
Possible Duplicate: Detect multitouch with Ontouch listener Android I have a question about detecting
Possible Duplicate: Detect Facebook page fan on the website I have a question to
Possible Duplicate: How to detect if a variable is an array I have a
Possible Duplicate: How to detect a click outside an element? I have a drop

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.