SoFunction
Updated on 2025-02-28

What is the relationship between property and prototype of a more detailed javascript object?

ECMAScript can recognize two types of objects. One is called Native Object belongs to the language category; the other is called Host Object, which is provided by the operating environment for example document objects.
Dom Node, etc.
Native objects are a loose structure and can dynamically increase properties. All properties have a name and a value. This value can be a reference to another object.
Or built-in data types (String, Number, Boolean, Null or Undefined)
The following simple example describes how a javascript object sets the value of a property and how to read the value of a property.

Assignment operation
The creation of attributes of an object is very simple, and the creation of attributes can be completed directly through assignment operations.
Code

   1. var objectRef = new Object(); //create a generic javascript object.  


A property named testNumber can be created like this.
Code

   1.  = 5;  
   2. /* - or:- */  
   3. objectRef["testNumber"] = 5;  


If the copied attribute name already exists, the attribute will not be created again. The assignment operation is just to reset the value of the attribute
Code

   1.  = 8;  
   2. /* - or:- */  
   3. objectRef["testNumber"] = 8;  



The prototype of the js object can also be an object, or it can have properties, assign the assignment operation of the js object and the creation of ordinary object properties.
Nothing different.

Value operation
In value operation, property and behavior are different. Let’s first look at the simplest property value operation.
Code

1./* Assign a value to an object's attribute. If the object does not have this attribute, then after the assignment operation, the object will have this attribute. */
   2.  = 8;  
3./* Read out the value of this property */
   4. var val = ;  
   5.   
6./* Now val get the value 8 given to objectRef just now*/


prototype revealed

However, all objects can have prototypes, and prototypes itself is also an object, so they can also have prototypes. In this way, a prototype chain will be formed if the loop continues.
This chain aborted when it encounters that the prototype of the formation in the chain is null. (Object's default prototype is null)
Code

   1. var objectRef = new Object(); //create a generic javascript object.  


Create a new js object. At this time, the prototype of this object is Null, so the prototype chain of objectRef only contains one object
We're looking at the following code
Code

1./* Construct the constructor of type MyObject1
   2.    MyObject1 - type. 
   3. */  
   4. function MyObject1(formalParameter){  
5.     /* Create a property name for the object called testNumber
   6.     */  
   7.      = formalParameter;  
   8. }  
   9.   
10./* Constructor of MyObject2 type
  11.    MyObject2 - type:- 
  12. */  
  13. function MyObject2(formalParameter){  
14.    /* Create a property name for the object called testString*/
  15.      = formalParameter;  
  16. }  
  17.   
18. /* The next step will replace the MyObject2 default prototype attribute with the MyObject1 object*/
  19.  = new MyObject1( 8 );  
  20.   
21. /*  Finally, we create an object of type MyObject2*/
  22.   
  23. var objectRef = new MyObject2( "String_Value" );  


objectRef This MyObject2 type object has a prototype chain. The first object in the chain is the MyObject1 object, and the MyObject1 object also has a prototype.
This prototype is the default prototype of Object, and the prototype is null, and the prototype chain ends.
When a value operation occurs, the entire prototype chain of objectRef starts working
Code

   1. var val = ;  


The objectRef has a property called testString, so this code will assign the value of testString to val
Code

   1. var val = ;  


The objectRef object does not have the testNumber attribute, but val reaches the value 8, not undefined. This is because the interpreter does not find the current object to be found.
After the property of , we will check the prototype of this object. The prototype of objectRef is MyObject1 object. This object has the property of testNumber, so val gets the value of 8.
Code

   1. var val = ;  


Now val is a reference to function, this function is a property. Since neither MyObject1 nor MyObject2 defines the property toString
So return.
Code

   1. var val = ;  


In the end, val is undefined, because MyObject1, MyObject2, and Object do not define the property madeUpProperty, so the result is undefined.

The read operation will read the first attribute value of the same name found on the obj itself and prototype chain.
Write operation will create an attribute with the same name for the obj object itself (if the attribute name does not exist
This means that = 3 will create a property on the objectRef object, name is testNumber, and the next time you want to read the testNumber
The property chain will not work, and will only get the property 3 of objectRef, and the testNumber property of MyObject1 will not be modified. The following code can be verified
Code

1./* Construct the constructor of type MyObject1
   2.    MyObject1 - type. 
   3. */  
   4. function MyObject1(formalParameter){  
5.     /* Create a property name for the object called testNumber
   6.     */  
   7.      = formalParameter;  
   8. }  
   9.   
10./* Constructor of MyObject2 type
  11.    MyObject2 - type:- 
  12. */  
  13. function MyObject2(formalParameter){  
14.    /* Create a property name for the object called testString*/
  15.      = formalParameter;  
  16. }  
  17.   
18. /* The next step will replace the MyObject2 default prototype attribute with the MyObject1 object*/
  19. var obj1 = new MyObject1( 8 );  
  20.  = obj1;  
  21.   
22. /*  Finally, we create an object of type MyObject2*/
  23.   
  24. var objectRef = new MyObject2( "String_Value" );  
  25.   
  26. alert();  
  27.  = 5;  
  28. alert();  
  29. alert();