More detailed JavaScript object's property and prototype what is a relationship _javascript skill

Source: Internet
Author: User
Tags object object
ECMAScript can recognize two types of objects, one of which is called native object, which belongs to the language category, and is called host object, provided by the running environment, such as a Document object,
Dom node, etc.
Native objects is a loosely structured and dynamically incremented property, with all attributes having a name and a value that can be a reference to another object
or a built-in data type (String, number, Boolean, Null, or Undefined)
The following simple example describes how a JavaScript object sets the value of a property and how it reads the value of a property.

Assignment operations
The creation of a property of an object is simple, and the creation of the property can be done directly through an assignment operation.
Code

1. var objectref = new Object (); Create a generic JavaScript object.


A property named Testnumber can be created this way.
Code

1. objectref.testnumber = 5;
2./*-or:-* *
3. objectref["Testnumber"] = 5;


If the copied property name already exists then the property is not created again, the assignment is simply to reset the property's value
Code

1. objectref.testnumber = 8;
2./*-or:-* *
3. objectref["Testnumber"] = 8;



JS object's prototype (prototype) itself can also be objects, can also have attributes (property), for JS object (prototype) assignment operations and normal object properties of the creation
Nothing different.

Value-taking operations
In the value operation of the property and prototype is not the same, first look at the simplest property value operation.
Code

1./* For an object of the property assignment, if this object does not have this attribute, then after the assignment operation, the object has this property.
2. Objectref.testnumber = 8;
3./* read out the value of this property * *
4. var val = objectref.testnumber;
5.
6./* Now Val has just given the value of ObjectRef 8.


Prototype

But all the objects can have prototypes, prototypes oneself is also object, then he also can have prototypes, so the circulation goes down to form a prototype chain,
This chain stops when he encounters the prototype of the formation in the chain that is null. (The default prototype for object is null)
Code

1. var objectref = new Object (); Create a generic JavaScript object.


Creates a new JS object, at which point the prototype of the object is null, so the OBJECTREF prototype chain contains only one object Object.prototype
We're looking at the code below.
Code

1./* Build MyObject1 constructor of this type
2. Myobject1-type.
3. */
4. Function MyObject1 (formalparameter) {
5./* To create a property for the object named Testnumber
6. */
7. This.testnumber = FormalParameter;
8.}
9.
10./* Build MyObject2 constructor of this type
myobject2-type:-.
12. */
function MyObject2 (formalparameter) {
14./* To create a property for the object named teststring*/
this.teststring = FormalParameter;
16.}
17.
18./* The next operation will replace the MYOBJECT2 default prototype property with the MyObject1 object.
Myobject2.prototype = new MyObject1 (8);
20.
21.//Finally we create an object of the MYOBJECT2 type/*
22.
var objectref = new MyObject2 ("String_value");


ObjectRef the object of this MyObject2 type has a chain of prototype, the first object in the chain is the MyObject1 object, and the MyObject1 object has prototype,
This prototype is the Object default Prototype,object.prototype prototype is NULL, and this prototype chain ends.
When a value operation occurs, the entire prototype chain of the objectref begins to work
Code

1. var val = objectref.teststring;


ObjectRef This object has a property called TestString, then this code assigns the value of TestString to Val
Code

1. var val = objectref.testnumber;


This attribute is not testnumber in the ObjectRef object, but Val is at value 8 instead of undefine, because the interpreter does not find the current object
, the prototype of the object's prototype,objectref is checked for the MyObject1 object, which has the Testnumber attribute, so Val gets the value of 8.
Code

1. var val = objectref.tostring;


Now Val is a function reference, this function is the property of Object.prototype, because MyObject1 and MyObject2 do not define ToString this property
So Object.prototype returned.
Code

1. var val = objectref.madeupproperty;


The last Val was undefined, because MyObject1 and MyObject2, and object did not define madeupproperty this property, so it was undefine.

The read operation reads the first attribute value found on Obj's own and prototype chains
Write creates a property of the same name for the Obj object itself (if the property name does not exist
This means that Objectref.testnumber = 3 Creates a property on the ObjectRef object, the name is Testnumber, and the next time you want to read the Testnumber
The Propertype chain will not work, just get ObjectRef property 3, and MyObject1 Testnumber properties will not be modified. The following code can verify
Code

1./* Build MyObject1 constructor of this type
2. Myobject1-type.
3. */
4. Function MyObject1 (formalparameter) {
5./* To create a property for the object named Testnumber
6. */
7. This.testnumber = FormalParameter;
8.}
9.
10./* Build MyObject2 constructor of this type
myobject2-type:-.
12. */
function MyObject2 (formalparameter) {
14./* To create a property for the object named teststring*/
this.teststring = FormalParameter;
16.}
17.
18./* The next operation will replace the MYOBJECT2 default prototype property with the MyObject1 object.
var obj1 = new MyObject1 (8);
Myobject2.prototype = obj1;
21st.
22.//Finally we create an object of the MYOBJECT2 type/*
23.
var objectref = new MyObject2 ("String_value");
25.
alert (Objectref.testnumber);
Objectref.testnumber = 5;
alert (Objectref.testnumber);
alert (Obj1.testnumber);
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.