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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:03:31+00:00 2026-06-10T03:03:31+00:00

What is prototype property, and why is it necessary? So far, I have learnt

  • 0

What is prototype property, and why is it necessary? So far, I have learnt that this provides public access to more intrinsic, and private prototype of the object; is that correct?

Also, what’s the difference between following statements?

MyConstructor.age = 30;
MyConstructor.prototype.age = 30;

In short, I need a better understanding of keyword prototype.

Thanks

  • 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-06-10T03:03:33+00:00Added an answer on June 10, 2026 at 3:03 am

    “Prototype” is something that plays a role in objects.

    In Javascript, everything is an object. Every object has a kind, and thus inherits the prototype of that kind.

    For example, take a simple array: var a = []. You can make operations with it, like a.push(10). Where does this push method come from? From the prototype of Array object, which a is.

    You can add your own methods to Array objects just by defining them in the prototype object. For example:

    Array.prototype.sortNum = function() {this.sort(function(a, b) {return a - b});};
    

    This way you can do something like a.sortNum() with all arrays, even the ones created before you defined the sortNum method.

    (Note: for compatibility reasons, it’s usually not recommended to extend the prototype of native objects like Arrays. But this particular example is usually a welcome addition, as well as normalizing methods like map and forEach for older browsers.)

    (Just never ever extend Object.prototype! Unless you don’t care to mess up for...in statements, the in operator and these sort of cases.)

    If you want to define your own classes, like the name MyConstructor suggests, you’ll have to define its prototype to define the methods for all the instances of that class:

    function MyConstructor(name) {this.name = name};
    MyConstructor.prototype = {
        print: function() {return this.name;}
    };
    
    var mc = new MyConstructor("foo");
    alert(mc.print()); // alerts "foo"
    

    You can define more than just functions in prototypes, too:

    MyConstructor.prototype.age = 30;
    
    alert(mc.age); // alerts 30
    

    Watch out when you do this to define “default” object values, because changing it may cause a change in all instances of that class.

    But this comes handy with Object.defineProperty:

    Object.defineProperty(MyConstructor.prototype, "wholeString", {
        get: function() {return this.name + "=" + this.age;},
        set: function(v) {this.name = v.substring(3);}
    });
    
    alert(mc.wholeString); // alerts "foo = 30"
    

    (Unfortunately, IE<9 allows this only for DOM objects…)

    When you define MyConstructor.age = 30 instead, what you’re actually doing is defining a member of the function MyConstructor, so mc.age would be undefined. Every instance of MyConstructor inherits the methods and members defined in MyConstructor.prototype, not the ones of the function MyConstructor.

    There’s much more to say, actually. Objects can be of a subclass of another class, thus inheriting the prototype of the superclass, too. For example, document.body is an instance of HTMLBodyElement, which is a subclass of HTMLElement, which is a subclass of Element and so on, until you get Object as the upmost superclass. So, document.body inherits all the methods defined in the prototype of HTMLBodyElement, HTMLElement, Element and Object. This is called the prototype chain.

    Doing the same with custom objects is a bit tricky:

    function Class() {};
    Class.prototype.foo = function() {alert("foo");};
    
    function Subclass() {};
    Subclass.prototype = new Class();
    Subclass.prototype.bar = function() {alert("bar");};
    
    var a = new Class(), b = new Subclass();
    a.foo(); // alerts"foo"
    a.bar(); // throws an error
    b.foo(); // alerts "foo"
    b.bar(); // alerts "bar"
    
    a instanceof Class;    // true
    a instanceof Subclass; // false
    b instanceof Class;    // true
    b instanceof Subclass; // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Math object does not have a prototype property, but does have a constructor
I have a small snippet of code that assigns a prototype property and an
I'm new to prototype-based languages and have read this question: Preserving a reference to
I just noticed that there is no prototype property on strings in JavaScript .
It's http://kan.pps.tv/web/pps.js PPStream.prototype.pps_width=400px; The PPStream is never declared, is this PPStream object purely generated
I see a lot of people doing this Object.prototype.foo = 'HALLO'; var hash =
With javascript, you can have one object inherit properties from another, so that if
I know that the prototype property of JavaScript function objects is copied to the
How Object.getPrototypeOf(obj) works? As per definition Object.getPrototypeOf(obj) should return prototype property of an Object
I like to learn the difference between Class Property and Prototype in Javascript what

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.