Web page Special Effects object Detailed application Example
1. When an attempt is to retrieve the value of a member element that does not exist for an object, a undefined value is returned. You can go through the "| |" operator to populate the default values:
stooge["Middle-name"]//undefined
var middle = stooge["Middle-name"] | | "Tom";
2. Attempting to retrieve a undefined value will result in an TypeError exception. You can use the "&&" operator to avoid errors:
Flight.equipment//undefined
Flight.equipment.model//throw "TypeError"
Flight.equipment && Flight.equipment.model//undefined
3. Objects are passed by reference. They will never be copied.
if (typeof object.beget!== ' function ')
{
Object.beget = function (o)
{
var f = function () {};
F.prototype = O;
return new F ();
}
}
var another_stooge = Object.beget (stooge);
A. The prototype connection does not work when it is updated
When we make a change to an object, we do not touch the object's prototype.
B. A prototype connection is only used when retrieving values
If you try to get a property value for an object that does not have this property name, get the property value from its Circle object. (If the prototype object also does not have the attribute value, then look for it from its prototype, and so on, to know the focus of the process left object.prototype.) If the desired attribute does not exist in the prototype chain at all, the result is the undefined value. This process is called a delegate)
C. Prototype relationships are a dynamic relationship.
If we add a new attribute to the prototype, the property is immediately visible to all objects created based on that prototype.
5. Reflection
At some point, we need to be able to process objects without knowing them at all, and discover their properties and methods before they are processed, a process called reflection (reflection).
You can use the "hasOwnProperty" method when determining whether an object has a property, and it returns true if the object has a unique property.
Note: The hasOwnProperty method does not check the prototype chain.
6. Enumeration
The for in statement can be used to traverse all property names in an object.
The order in which the property names appear is indeterminate, so be prepared for any sequence that might occur. If you want to make sure that the attributes appear in a particular order, the best thing to do is to avoid using the for in statement, and instead create an array in which to include the property name in the correct order
Use the For in statement:
var name;
for (name in Another_stooge)
{
if (typeof Another_stooge[name]!== ' function ')
{
Document.writeln (name + ': ' + another_stooge[name]);
}
}
//Use for statement
var i;
var properties = [
' First-name ',
' Middle-name ' ,
' Last-name ',
' profession '
]
for (i=0; i<properties.length; i +=1)
{
document.writeln (properties[i] + ': ' +
another_ Stooge[properties[i]]);
}