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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:38:26+00:00 2026-05-13T08:38:26+00:00

There are two ways of creating an object in javascript: use new on a

  • 0

There are two ways of creating an object in javascript:

  1. use new on a “constructor function”
  2. return a dictionary {} and set the proper key/value pairs

The first is, for example

FooType = function() {
    this.hello = function() { alert("hello"); };
};

foo = new FooType();
foo.hello();

the second is

fooFactory = function() {
    return { 
        hello : function() { alert("hello"); }
    };
};

foo = fooFactory();
foo.hello();

(Code written for the post. not guaranteed correct)

Apart from risks of mistakes of having this bound to the global object, are these two method totally equivalent (also considering prototype inheritance etc…)?

  • 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-13T08:38:26+00:00Added an answer on May 13, 2026 at 8:38 am

    They’re not equivalent, especially when considering prototype inheritance.

    FooType = function() {
        this.hello = function() { alert("hello"); };
    };
    
    var foo = new FooType();
    
    FooType.prototype.bye = function() { alert('bye!'); };
    
    foo.bye(); // "bye!"
    

    The only way you could achieve that in the fooFactory way would be to add it to the object prototype which is a Very Bad Idea.

    The first method is much more meaningful in my opinion (since the object has a type you can check against) and can offer much better performance if the prototype is done properly. In your first example, every time you instantiate a new FooType object, it create a new “hello” function. If you have lots of these objects, that’s a lot of wasted memory.

    Consider using this instead:

    function FooType() { }
    FooType.prototype.hello = function() {
        alert('Hello');
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference between following two ways of creating an object. Student s1
I know that there are several ways to define a function in JavaScript. Two
while creating a thread in java, there is two ways such as Extending threads
To add a usercontrol in codebehind there are two ways. Exposing a usercontrol constructor
As far as I can tell, there are two main ways of creating functions
There are two ways of creating a custom HTML helper in ASP.NET MVC: You
recently I have come across these two ways of creating an object in a
There are two ways to overload operators for a C++ class: Inside class class
There are two ways to write a polymorphic migration in Rails. Generally, I've done
I know there are two ways, [self.navigationController pushViewController:siteViewController animated:YES]; [self presentModalViewController:anotherViewController animated:YES]; However, those

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.