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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:34:28+00:00 2026-05-18T12:34:28+00:00

Has anyone noticed this behavior before? This really threw me off… I would have

  • 0

Has anyone noticed this behavior before? This really threw me off… I would have expected prototyped arrays to be private to each class instance rather than shared between all class instances.

Can someone verify that this is the correct behavior and perhaps explain this behavior in more detail?

Notice the commented code and how it affects the behavior of the script.

<html>
<head>

<script type="text/javascript">

function print_r( title, object ) {

    var output = '';
    for( var key in object ) {

        output += key + ": " + object[ key ] + "\n";

    }

    output = title + "\n\n" + output;

    alert( output );

}

function Sandwich() {

    // Uncomment this to fix the problem
    //this.ingredients = [];

}

Sandwich.prototype = {

    "ingredients" : [],
    "addIngredients" : function( ingArray ) {

        for( var key in ingArray ) {

            this.addIngredient( ingArray[ key ] );

        }

    },
    "addIngredient" : function( thing ) {

        this.ingredients.push( thing );

    }

}

var cheeseburger = new Sandwich();
cheeseburger.addIngredients( [ "burger", "cheese" ] );

var blt = new Sandwich();
blt.addIngredients( [ "bacon", "lettuce", "tomato" ] );

var spicy_chicken_sandwich = new Sandwich();
spicy_chicken_sandwich.addIngredients( [ "spicy chicken pattie", "lettuce", "tomato", "honey dijon mayo", "love" ] );

var onLoad = function() {

    print_r( "Cheeseburger contains:", cheeseburger.ingredients );

};

</script>

</head>
<body onload="onLoad();">
</body>
</html>

Many 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-05-18T12:34:29+00:00Added an answer on May 18, 2026 at 12:34 pm

    The prototype of an object is just an object. The prototype properties are shared between all objects that inherit from that object. No copies of the properties are made if you create a new instance of a “class” (classes don’t exist anyway in JS), i.e. an object which inherits from the prototype.

    It only makes a difference on how you use the these inherited properties:

    function Foo() {}
    
    Foo.prototype = {
        array: [],
        func: function() {}
    }
    
    a = new Foo();
    b = new Foo();
    
    a.array.push('bar');
    console.log(b.array); // prints ["bar"]
    
    b.func.bar = 'baz';
    console.log(a.func.bar); // prints baz
    

    In all these cases you are always working with the same object.

    But if you assign a value to a property of the object, the property will be set/created on the object itself, not its prototype, and hence is not shared:

    console.log(a.hasOwnProperty('array')); // prints false
    console.log(a.array); // prints ["bar"]
    a.array = ['foo'];
    console.log(a.hasOwnProperty('array')); // prints true
    console.log(a.array); // prints ["foo"]
    console.log(b.array); // prints ["bar"]
    

    If you want to create own arrays for each instance, you have to define it in the constructor:

    function Foo() {
        this.array = [];
    }
    

    because here, this refers to the new object that is generated when you call new Foo().

    The rule of thumb is: Instance-specific data should be assigned to the instance inside the constructor, shared data (like methods) should be assigned to the prototype.


    You might want to read Details of the object model which describes differences between class-based vs. prototype-based languages and how objects actually work.

    Update:

    You can access the prototype of an object via Object.getPrototypeOf(obj) (might not work in very old browsers), and Object.getPrototypeOf(a) === Object.getPrototypeOf(b) gives you true. It is the same object, also known as Foo.prototype.

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

Sidebar

Related Questions

Has anyone noticed this behavior? I'm trying to write a script that will trigger
I don't know if anyone ELSE has noticed this, but I noticed the jQuery
I don't know if anyone else even has this problem, or has noticed, but
Has anyone run into this error message before when using a timer on an
I don't know if anyone has noticed that Eclipse has this annoying feature where
Anyone who has programmed with actionscript 3.0 has most certainly noticed its lack of
I have noticed an undesirable behavior with .net winforms applications. I have a wide
I have this basic one-to-many relationship in Core Data: Brand has N Products Product
Just wondering if anyone has encountered this and has a good fix. Here is
Has anyone not noticed that JQuery uses ActiveX controls? When a user has limited

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.