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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:55:06+00:00 2026-05-27T19:55:06+00:00

I am learning Object Oriented Java Script. I have below code for Factory Method.

  • 0

I am learning Object Oriented Java Script. I have below code for Factory Method.

function Foo() {
    var value = 1;
    return {
        method: function() {
            return value;
        },
        value:value
    }
}
Foo.prototype = {
    bar: function() {}
};

new Foo();
Foo();

method Foo can be called by two ways. new Foo(); or Foo(); Both do the same thing and output is same. what is actual difference in java script processing ?

  • 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-27T19:55:07+00:00Added an answer on May 27, 2026 at 7:55 pm

    In normal cases, new should be used when creating objects from a constructor function and eschewed when performing any other function call. When using new on a function 1) a new object is created; 2) the function is called with the value of this bound to that new object, and 3) the value returned from the function (by default) is the object created in step one.

    However, in your case you’re returning a completely new object from the “constructor” (different from the object in 1) above), which means there is no practical difference. The value of this will still be different inside the function, but both of these will return false:

    new Foo() instanceof Foo;
    Foo() instanceof Foo;
    

    To illustrate the this difference, add the following to Foo:

    alert(this instanceof Foo)
    

    When called with new this alerts true; when called without it, false.

    Furthermore, there’s no point in assigning an object to Foo.prototype because you’ll never create any instances of Foo that would make use of it (because, again, you’re returning something completely different from Foo).

    If you’re going to return a custom object from Foo then you should prefer the version without new; there’s no point in creating a new instance of Foo if you’re going to ignore it and return something completely different, anyway.

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

Sidebar

Related Questions

I have recently started learning java as my first ever step into object oriented
I'm learning Objective-C, and have a C/C++ background. In object-oriented C++, you always need
learning about loops (still a beginner) in VB.net. I have got the below code
Here is a problem I've struggled with ever since I first started learning object-oriented
I come from classes object orientation languages and recently I have been learning those
I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
I'm learning about DDD, and have come across the statement that value-objects should be
I am designing an object-oriented programming language for the purpose of learning. The language
I am new to taking an object oriented approach with php...actually I have a
This past semester I took intro to object oriented programming in java and next

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.