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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:01:13+00:00 2026-05-24T10:01:13+00:00

When doing inheritance in JavaScript, the pattern that I always see defines instance methods

  • 0

When doing inheritance in JavaScript, the pattern that I always see defines instance methods in the prototype, but instance fields in the constructor (see example below). What is the motivation for this? Why not be consistent and define both in the prototype?

function MyClass() {
    this.myField = 0; // why this...
}
MyClass.prototype.myField = 0; // ...instead of this?
  • 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-24T10:01:13+00:00Added an answer on May 24, 2026 at 10:01 am

    Explanation

    Because prototype properties are shared between all instances as every instance has a reference to the same prototype object.

    This is not an issue with immutable types, you could do:

    MyClass.prototype.myField = 0;
    
    var a = new MyClass();
    a.myField = 42;
    

    and only a would have this value. This is because the assignment creates the property myField for a. You can test this with calling a.hasOwnProperty('myField') before and after the assignment.

    But if you have objects or arrays

    MyClass.prototype.myField = [];
    

    and you just append to that array and don’t assign a new array (like a.myField = []), then every instance has the new value in that array.


    Solution

    You have to initialize arrays and objects in the constructor, so that each instance gets its own object or array instance. Some style guidelines propose that you still create the property on the prototype, but initialize it with null. This has no benefit other than adding some conceptual structure (if such a word exists) to your code.

    For example:

    function MyClass() {
        this.myField = [];
    }
    
    /**
     * @type {Array}
     */
    MyClass.prototype.myField = null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing OO javascript for the first time. I have read about inheritance
I'm doing some per table inheritance and all is working great- but I'm noticing
I'm using the JS prototype inheritance pattern from Gavin Kistner and I'm not quite
I've been looking into doing inheritance in JavaScript the correct prototypal way, according to
I am doing Object Oriented programming in JavaScript without Prototype/jQuery (I use jQuery for
I see a lot of people doing this Object.prototype.foo = 'HALLO'; var hash =
I'been doing some inheritance in js in order to understand it better, and I
In JavaScript what sort of inheritance do you favour? The ECMA standard is to
I am trying inherit objects in JavaScript. Take a look at this example: var
I have an issue with some inheritance I'm doing. Basically, I have an abstract

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.