This article summarizes the definition, use, and object and prototype chain operations of objects in JavaScript. Share to everyone for your reference, specific as follows:
1. In addition to the 5 basic types, the rest of JS is the object
(1) The definition of the object:
Direct definition:
New Way to create:
var test=new Object ({x:1})
Object.create Way:
var test=object.create ({x:1})
(2) Assignment and value on the object
Example:
Method One: You can use test[' x ' to take value
Method Two: Can take value by test.x
Essence, will [] inside the value of ToString to take, for example, we have here the assignment statement such as:
You can get a value of 3 by Test[{z:1}], and if you have 5 basic types, you want to take the same value.
In short, the value inside the test is stored after the ToString method is invoked.
2. Object and prototype chain
(1) Here is involved in the inheritance of JS, JS objects In addition to NULL, the other are inherited from the prototype object Object.prototype, that is, JS most of the object of the prototype chain on the top has an object object.prototype.
JS is integrated based on the prototype chain, and the object can access the properties and methods of the object on the prototype chain.
Example:
function test () {
this.x=x;
This.y=y
}
var my=new test ();
test.prototype.z=3;
alert (my.z); Output is 3
Test.prototype is the direct parent chain prototype of my, and I can also search along the prototype chain until Object.prototype, so that the methods in Object.prototype can be implemented, such as
My.tostring ();//Return [Object Object]
(2) The influence of prototype chain on object traversal
We have defined an object I above, and if the object is traversed with a for in, then there will be
function test () {
this.x=1;
this.y=2;
}
var my=new test ();
test.prototype.z=3;
Console.log (my.__proto__)//Will output test.prototype for
(p. My)
{
console.log (my[p])//Will output 1,2,3
}
We found that the value of the my[p] was finally 1,2,3 and found that the properties on the My object's prototype chain were traversed when traversing the properties on my.
Added: And this traversal is unordered.
(3) Special assignment
For the above example, if we assign a value to the My object,
my.z=4;
alert (my.z);//This gets a value of 4
We found that if the objects on the object conflict with the properties on the target, precedence is assigned to the properties on the object.
(4) In the object's prototype chain, all objects are inherited and Object.prototype, in addition to NULL, we can use the following chain to react:
My (or other object)->test.prototype–>........–>object.prototype-->null
For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills summary, javascript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical Operational Usage Summary
I hope this article will help you with JavaScript programming.