About the problem of modifying the property values of prototype objects through instance objects in JavaScript

Source: Internet
Author: User

There are two main classes of data values in javascript: The data values of the base type and the data values of the reference type.

There are 5 types of data values for the base type: null, Undefined, number, Boolean, and string.

The data value of the reference type is said to be 1, that is, the object type. To be fine, there are: Object type, array type, date type, regexp type, function type, and so on.

When the property value of a prototype object is a data value of the base type, it does not occur if the property value is modified by an instance object, causing the property value of the prototype object to change. When the property value of a prototype object is a data value of a reference type, modifying the property value through an instance object may cause the property value of the prototype object to change. The following examples illustrate.

Example 1:

 function   Animal () {}animal.prototype  = {constructor:animal,number:  "very much" " Shark "," sardine " "Fly" " feather " var  animal1 = new   Animal ();  var  animal2 = new   Animal ();  //  Animal1.number = 1000 ;console.log (Animal2.number);  // very much  

In the previous example, two instance objects were created through the constructor animal, and two instance objects inherit the properties of the same prototype object. The Number property is Animal1 with the instance object, and the result is that the instance object animal1 has its own # property, and does not change the value of the prototype object's # property. The number property of the instance object Animal2 call is also the original number property of the prototype object.

Example 2:

functionAnimal () {}animal.prototype={constructor:animal, Number:"Very much", fish: ["Shark", "sardine"], bird:{ability:"Fly", Feature:"Feather" }};varAnimal1 =NewAnimal ();varAnimal2 =NewAnimal ();varAnimal3 =NewAnimal ();//The fish attribute in the prototype is not rewritten, and the Animal1 instance object has its own fish attribute, and pushing and ejecting into its own fish property does not change the fish properties of the prototype. Animal1.fish = ["Cold Fish"]; for(vari=0;i<animal2.fish.length;i++) {Console.log (animal2.fish[i]);//shark,sardine, no cold fish. }//Animal3 the Fish property of the prototype object by pushing the item into the fish attribute through the instance object, because the instance object does not have its own fish attributeAnimal3.fish.push ("Voladao");
Animal3.fish[0] = "Fly fish"; for(vari=0;i<animal2.fish.length;i++) {Console.log (animal2.fish[i]);// Fly Fish, Sardine,voladao}

In Example 2, the instance object Animal1 created its own fish property without altering the fish properties of the prototype object, so the instance object animal2 the fish attribute of the prototype object.

The instance object Animal3 does not have its own fish attribute, but it pushes an item into the fish attribute through the instance object Animal3, and changes the value of the first item in it, which occurs on the fish property of the prototype object, so the instance object Animal2 when the fish attribute is called. Its property value has changed.

Example 3:

functionAnimal () {}animal.prototype={constructor:animal, Number:"Very much", fish: ["Shark", "sardine"], bird:{ability:"Fly", Feature:"Feather" }}varAnimal1 =NewAnimal ();varAnimal2 =NewAnimal ();varAnimal3 =NewAnimal ();varAnimal4 =NewAnimal ();varANIMAL5 =NewAnimal ();//rewritten the Bird property in the prototypeanimal1.bird.ability = "Run"; Console.log (animal2.bird.ability);//Run//Create animal3 Bird property without changing the bird property of the prototype objectAnimal3.bird ={eat:"Fish" }; Console.log (animal4.bird.eat);//undifinedConsole.log (animal3.bird.eat);//FishAnimal5.bird.home = "Tree"; Console.log (animal4.bird.home);//Tree

In Example 3, the value of the ability property of the bird property is modified by the instance object Animal1, and the instance object Animal1 does not have its own bird property, which is reflected on the bird property of the prototype object. The value of the bird.ability of the instance object Animal2 output is the changed value.

Instance Object Animal3 creates its own bird property, which does not change the bird property of the prototype object, so the Bird.eat value of instance object 4 is undifined.

By adding the home property of the bird property through the instance object ANIMAL5, the instance object ANIMAL5 has no bird property of its own, and the home property is added to the bird property of the prototype object, so Animal4 's bird.home value is tree.

As for the other reference types of data values have no above features, not yet thought how to verify, first of all.

About the problem of modifying the property values of prototype objects through instance objects in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.