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.
var objectref = new Object (); Create a generic JavaScript object.
A property named Testnumber can be created this way.
Objectref.testnumber = 5;
/*-or:-* *
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
Objectref.testnumber = 8;
/*-or:-* *
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.
/* Assign a value to an object's properties, and if the object does not have this attribute, then the object has this property after the assignment operation.
Objectref.testnumber = 8;
/* read out the value of this attribute/*
var val = objectref.testnumber;
/* Now Val has given objectref the value of 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)
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 following
/* Build MyObject1 constructor of this type
Myobject1-type.
*/
function MyObject1 (formalparameter) {
/* Creates a property for a person object named Testnumber
*/
This.testnumber = FormalParameter;
}
/* Build MyObject2 constructor of this type
myobject2-type:-
*/
function MyObject2 (formalparameter) {
/* Creates a property for a person object named teststring*/
this.teststring = FormalParameter;
}
/* The next action replaces the MYOBJECT2 default prototype property with the MyObject1 object.
Myobject2.prototype = new MyObject1 (8);
/* Finally, we created an object of the MYOBJECT2 type * *
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
var val = objectref.teststring;
ObjectRef This object has a property called teststring, then this sentence will assign the value of TestString to Val
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.
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.
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 can verify
/* Build MyObject1 constructor of this type
Myobject1-type.
*/
function MyObject1 (formalparameter) {
/* Creates a property for a person object named Testnumber
*/
This.testnumber = FormalParameter;
}
/* Build MyObject2 constructor of this type
myobject2-type:-
*/
function MyObject2 (formalparameter) {
/* Creates a property for a person object named teststring*/
this.teststring = FormalParameter;
}
/* The next action replaces the MYOBJECT2 default prototype property with the MyObject1 object.
var obj1 = new MyObject1 (8);
Myobject2.prototype = obj1;
/* Finally, we created an object of the MYOBJECT2 type * *
var objectref = new MyObject2 ("String_value");
alert (Objectref.testnumber);
Objectref.testnumber = 5;
alert (Objectref.testnumber);
alert (Obj1.testnumber);
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.