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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:11:41+00:00 2026-05-16T18:11:41+00:00

The new keyword in JavaScript can be quite confusing when it is first encountered,

  • 0

The new keyword in JavaScript can be quite confusing when it is first encountered, as people tend to think that JavaScript is not an object-oriented programming language.

  • What is it?
  • What problems does it solve?
  • When is it appropriate and when not?
  • 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-16T18:11:42+00:00Added an answer on May 16, 2026 at 6:11 pm

    It does 5 things:

    1. It creates a new object. The type of this object is simply object.
    2. It sets this new object’s internal, inaccessible, [[prototype]] (i.e. __proto__) property to be the constructor function’s external, accessible, prototype object (every function object automatically has a prototype property).
    3. It makes the this variable point to the newly created object.
    4. It executes the constructor function, using the newly created object whenever this is mentioned.
    5. It returns the newly created object, unless the constructor function returns a non-null object reference. In this case, that object reference is returned instead.

    Note: constructor function refers to the function after the new keyword, as in

    new ConstructorFunction(arg1, arg2)
    

    Once this is done, if an undefined property of the new object is requested, the script will check the object’s [[prototype]] object for the property instead. This is how you can get something similar to traditional class inheritance in JavaScript.

    The most difficult part about this is point number 2. Every object (including functions) has this internal property called [[prototype]]. It can only be set at object creation time, either with new, with Object.create, or based on the literal (functions default to Function.prototype, numbers to Number.prototype, etc.). It can only be read with Object.getPrototypeOf(someObject). There is no other way to get or set this value.

    Functions, in addition to the hidden [[prototype]] property, also have a property called prototype, and it is this that you can access, and modify, to provide inherited properties and methods for the objects you make.


    Here is an example:

    ObjMaker = function() { this.a = 'first'; };
    // `ObjMaker` is just a function, there's nothing special about it
    // that makes it a constructor.
    
    ObjMaker.prototype.b = 'second';
    // like all functions, ObjMaker has an accessible `prototype` property that 
    // we can alter. I just added a property called 'b' to it. Like 
    // all objects, ObjMaker also has an inaccessible `[[prototype]]` property
    // that we can't do anything with
    
    obj1 = new ObjMaker();
    // 3 things just happened.
    // A new, empty object was created called `obj1`.  At first `obj1` 
    // was just `{}`. The `[[prototype]]` property of `obj1` was then set to the current
    // object value of the `ObjMaker.prototype` (if `ObjMaker.prototype` is later
    // assigned a new object value, `obj1`'s `[[prototype]]` will not change, but you
    // can alter the properties of `ObjMaker.prototype` to add to both the
    // `prototype` and `[[prototype]]`). The `ObjMaker` function was executed, with
    // `obj1` in place of `this`... so `obj1.a` was set to 'first'.
    
    obj1.a;
    // returns 'first'
    obj1.b;
    // `obj1` doesn't have a property called 'b', so JavaScript checks 
    // its `[[prototype]]`. Its `[[prototype]]` is the same as `ObjMaker.prototype`
    // `ObjMaker.prototype` has a property called 'b' with value 'second'
    // returns 'second'
    

    It’s like class inheritance because now, any objects you make using new ObjMaker() will also appear to have inherited the ‘b’ property.

    If you want something like a subclass, then you do this:

    SubObjMaker = function () {};
    SubObjMaker.prototype = new ObjMaker(); // note: this pattern is deprecated!
    // Because we used 'new', the [[prototype]] property of SubObjMaker.prototype
    // is now set to the object value of ObjMaker.prototype.
    // The modern way to do this is with Object.create(), which was added in ECMAScript 5:
    // SubObjMaker.prototype = Object.create(ObjMaker.prototype);
    
    SubObjMaker.prototype.c = 'third';  
    obj2 = new SubObjMaker();
    // [[prototype]] property of obj2 is now set to SubObjMaker.prototype
    // Remember that the [[prototype]] property of SubObjMaker.prototype
    // is ObjMaker.prototype. So now obj2 has a prototype chain!
    // obj2 ---> SubObjMaker.prototype ---> ObjMaker.prototype
    
    obj2.c;
    // returns 'third', from SubObjMaker.prototype
    
    obj2.b;
    // returns 'second', from ObjMaker.prototype
    
    obj2.a;
    // returns 'first', from SubObjMaker.prototype, because SubObjMaker.prototype 
    // was created with the ObjMaker function, which assigned a for us
    

    I read a ton of rubbish on this subject before finally finding this page, where this is explained very well with nice diagrams.

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

Sidebar

Related Questions

I like the new Dynamic keyword and read that it can be used as
First, I am not talking about the new 'dynamic' keyword n C# 4.0. :)
I am trying to create a JavaScript regular expression that can find a keyword
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure:
I understand how the new keyword can hide methods in a derived class. However,
Guids are created using the new keyword which makes me think it's a reference
People always talk about how objects created without the new keyword are destroyed when
I have quite a few problems, probably misunderstandings, about the new keyword with properties
I understand that the new ‘dynamic’ keyword in C# 4.0 facilitates interaction with dynamic
I am very new in Javascript. I want to make some basic stuffs that

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.