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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:25:55+00:00 2026-06-12T14:25:55+00:00

This is a newbie query. I went through few similar posts, but they are

  • 0

This is a newbie query. I went through few similar posts, but they are not helping me enough. This post has 2 queries, but am putting together because their root seems the same.

I came across the following code snippet :

var Server = module.exports.Server = function Server(listener) {
  if (!(this instanceof Server)) return new Server(listener);
  //other code
}

module.exports.createServer = function(listener) {
  return new Server(listener);
};

I am unable to understand the use of if (!(this instanceof Server)) ; when can this not point to Server here ?

I tried putting a quick test for this :

var createTest = function(){
    console.log(this.toString());
    return new Test();
};

var Test = function Test(){
    console.log(this instanceof Test);
    console.log(this.toString());
    if (!(this instanceof Test)) 
    {
        return new Test();
    }
}

var tester = createTest();

which outputs :

[object global]
true
[object Object]

which further confuses me over why this.toString prints [object Object] – shouldn’t it be [object Test] ?

Thanks !

  • 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-12T14:25:56+00:00Added an answer on June 12, 2026 at 2:25 pm

    That’s an idiom used to make new optional. For example:

    function TraditionalTest() {
    }
    function IdiomaticTest() {
        if(!(this instanceof IdiomaticTest)) return new IdiomaticTest();
    }
    
    console.log(new TraditionalTest());  // [object Object]
    console.log(TraditionalTest());      // undefined
    console.log(new IdiomaticTest());    // [object Object]
    console.log(IdiomaticTest());        // [object Object]
    

    As for why it’s [object Object] rather than [object Test], I don’t know why it’s defined that way, but that’s the way it’s defined:

    When the toString method is called, the following steps are taken:

    1. If the this value is undefined, return “[object Undefined]“.
    2. If the this value is null, return “[object Null]“.
    3. Let O be the result of calling ToObject passing the this value as the argument.
    4. Let class be the value of the [[Class]] internal property of O.
    5. Return the String value that is the result of concatenating the three Strings “[object“, class, and “]“.

    [[Class]] is referenced several times in the specification, but it only ever represents built-in types:

    • Arguments
    • Array
    • Boolean
    • Date
    • Error
    • Function
    • JSON
    • Math
    • Number
    • Object
    • RegExp
    • String
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question will sound like a newbie but i have gone through all threads
This has been bugging me for some time now, as a newbie I'm not
Sorry for this newbie question, but I can't find on google what I need
I know that this is a newbie question but I am a newbie so
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I am newbie to db programming and need help with optimizing this query: Given
I'm newbie in t-sql, and I wonder why this query executes so long ?
I'm not a db expert and this is for sure a newbie question. I've
This is a very standard newbie question, but I am looking for an expert
tsql newbie here. I have a table, similar to this one: CarId CarName UserId

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.