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

  • Home
  • SEARCH
  • 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 7556803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:57:24+00:00 2026-05-30T11:57:24+00:00

I am very confused about how constructors work in JavaScript; despite using the language

  • 0

I am very confused about how constructors work in JavaScript; despite using the language for several years (mostly as if it were like a semi-imperative version of Lisp) I would like to know more about how objects are supposed to work in it.

Given this code:

function Foo(x) {
    return {
        bar: function() { return x; }
    };
}

What is the difference between calling myFoo = Foo(5) and myFoo = new Foo(5)? Or, in other words, what exactly does a constructor in JavaScript do?

  • 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-30T11:57:25+00:00Added an answer on May 30, 2026 at 11:57 am

    What is the difference between calling myFoo = Foo(5) and myFoo = new Foo(5)?

    There’s no difference for that code, because it returns an object, and the spec says:

    • Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
    • If Type(result) is Object then return result.

    Since that function returns a result that is an Object, its result is used. You would notice a difference if it did not return an object, or if it checked this, for example if you rewrote it as:

    function Foo(x) {
      if (!(this instanceof Foo)) { return new Foo(x); }
      this.bar = function() { return x; };
    }
    // Now instanceof works.
    alert((new Foo) instanceof Foo);
    

    What does new in JavaScript do, anyway?

    The new operator causes the function to be called with this bound to a newly created Object whose prototype is that function’s prototype property.

    For user-defined functions,

    new f(a, b, c)
    

    is equivalent to

    // Create a new instance using f's prototype.
    var newInstance = Object.create(f.prototype), result;
    
    // Call the function
    result = f.call(newInstance, a, b, c),
    
    // If the result is a non-null object, use it, otherwise use the new instance.
    result && typeof result === 'object' ? result : newInstance
    

    Note, that the language specification actually defines functions with two operations, [[Call]] and [[Construct]], so there are some corner cases where new behaves oddly.

    For example, bound and built-in functions:

    var g = f.call.bind(f);
    

    should define a function that when called, just calls f, so g should be the same as f in all respects, but

    new g()
    

    produces

    TypeError: function call() { [native code] } is not a constructor
    

    because the builtin function Function.prototype.call supports [[Call]] but not [[Construct]].

    Function.prototype.bind also behaves differently around new and regular calls. The this value is always the bound thisValue when called, but is a newly constructed instance when you use new.

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

Sidebar

Related Questions

Ok, I'm now very confused. After my last question had several people comment about
I am very confused about the const version and non-const version member function like
I am very confused about how decimal numbers work in perl. I'm having trouble
I'm very confused about using destructors in Qt4 and hope, you guys can help
I'm very confused about behaviour of PHP's left shift function. I'm using it on
I'm very confused about the whole user profile thing in drupal, hope someone can
I'm VERY confused as to why this code Html.ActionLink(About, About, Home, new { hidefocus
Very confused here, trying out the yuicompressor on a simple javascript file. My js
I'm very confused about this issue and can't understand it.In the Enumerable Documentation, I
This question expands upon the one at abstract-class-numberformat-very-confused-about-getinstance . I feel that this question

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.