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

The Archive Base Latest Questions

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

I’m trying to understand exactly what the javascript new keyword means, why it is

  • 0

I’m trying to understand exactly what the javascript new keyword means, why it is necessary and when I should use it.

Consider the following examples:

      var x = new function(){
        var self=this;
        this.myFunction = function(){alert('foo ' + self.v)}
        this.v='x';
      };

      var y = function(){
        var self=this;
        this.myFunction = function(){alert('foo ' + self.v)}
        this.v='y';
        return this;
      }();


      var f=function(){
        var self=this;
        this.myFunction = function(){alert('foo ' + self.v)}
        this.v='z';
      }

      var z = new f();

      x.myFunction();
      y.myFunction();
      z.myFunction();

x,y and z are all objects. Potentially with public and private member variables.

x and z where constructed with the use of the new keyword. As far as I can tell, the new keyword simply executes the function and returns the context. So I guess in the above example x and y are essentially singletons since any reference to the original function is lost?

Apart from that are they all exactly equivalent? If not what are the differences and when would I want to use one approach or avoid another?

Thanks for any explanations.

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

    I’m trying to understand exactly what the javascript new keyword means

    a = new X() means something like:

    • create new object A
    • set its prototype to the object called X.prototype (all functions have a “prototype” property which is just an object and you can modify it to add behaviour to objects created using this function and new),
    • call X with this=a to initialize the object.

    This is similar to:

    a = Object.create(/* prototype */ X.prototype, {});
    a.constructor = X; /* magic property which can be read after */
    X.apply(/* this */ a, /* args */[]);
    

    why it is necessary and when I should use it.

    It isn’t and you don’t have to. Object.create works more naturally with JS prototype-based object model and lets you create arbitrary prototype chains.

    When new is used, it’s used like this:

    function Class(constructor_args) {
       // init an object
       this.a = 10;
    };
    Class.prototype.method1 = function() {
       console.log(this.a);
    }
    var obj = new Class();
    

    Then after you create an object using new Class(), its prototype refers to the object Class.prototype which contains its methods while the object itself contains the fields.

    (Personal opinion: The keyword new was introduced to make it easier in Javascript to write code which resembles Java/C++ and classical non-prototypal object model.)

    Plase read these excellent articles:

    Classical inheritance in Javascript

    Prototypal inheritance

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.