Home > Web Front-end > JS Tutorial > Are JavaScript Object Members Prototyped as Arrays Shared or Private?

Are JavaScript Object Members Prototyped as Arrays Shared or Private?

Linda Hamilton
Release: 2024-11-14 18:43:02
Original
1044 people have browsed it

Are JavaScript Object Members Prototyped as Arrays Shared or Private?

JavaScript Object Members Prototyped as Arrays: Shared or Private?

In JavaScript, object properties can be defined in two ways: directly on the object or as properties of the object's prototype. This behavior has significant implications when dealing with arrays.

By default, prototyped arrays are shared among all instances of a class, leading to unexpected behavior. For instance, if the prototype of a class contains an empty array, all instances will reference the same array. This can be observed in the following example:

function Sandwich() {
  // Uncomment 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"]);

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

In this example, both cheeseburger and blt share the same ingredients array, even though they are different instances. Adding an ingredient to cheeseburger also affects blt, highlighting the shared nature of the array.

To rectify this behavior, it is recommended to define arrays directly on the object itself, rather than as prototype properties. This ensures that each instance has its own private array. The following modification to the code fixes the problem:

function Sandwich() {
  this.ingredients = [];
}
Copy after login

By defining the array in the constructor, each instance of Sandwich will have its own private copy of the array, eliminating the sharing behavior.

In summary, prototyped arrays are shared among all instances of a class in JavaScript. To create private arrays for each instance, it is necessary to define the array directly on the object itself in the constructor. This distinction is crucial for understanding and managing object behavior in JavaScript.

The above is the detailed content of Are JavaScript Object Members Prototyped as Arrays Shared or Private?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template