Creating objects
• Direct volume of objects
• constructors
• Prototype inheritance
var p = object.create (o);
Class inheritance
JavaScript objects have their own properties and inherited properties.
• When querying the property X of Object o, first look for the attribute x in O, and if not, find the X attribute in the prototype of O until you find an X or an object with a prototype that is null
• If you assign a value to the X property of object o, if there is already a property x in O, change the value of x, and if there is no property x in O, create an X attribute and assign it to O
In other words, the prototype chain works only when the query is in effect.
var O = {
x:1
};
function P () {
this.y = 2;
}
P.prototype = O;
var t = new P ();
Console.log (t);
Console.log (' x ' in t);//true
Console.log (T.hasownproperty (' x '));//false
You can use in or hasownproperty to determine whether an attribute exists in an object.
Object Properties
• Traverse Object Properties
You can use for.. In to traverse the properties of the object
Use for.. In, the attributes on the prototype chain are traversed. The traversal order is the breadth-first traversal
Therefore, the use of hasOwnProperty can determine whether the object has its own properties.
• Attributes of Object properties
Use Object.getownpropertydescriptor () to get descriptors for object-specific properties
Writable (writable) indicates whether an object property is writable
For example
var o = {
foo: ' Bar '
}
object.defineproperty (o, "foo", {writable:false});
O.foo = ' world ';
Console.log (O.foo)//Still output bar
Enumerable (enumerable) indicates whether an object property can be enumerated
For example
The enumerable of attributes such as length in array is false, so
For (p in Array) {
console.log (p);
}
Do not output anything
Configurable (configurable) indicates the possibility of modifying the property's scalability and enumerable
You can use Object.defineproperties to define these configuration properties.
Object.defineproperty (o, "foo", {writable:false});
Get represents a method of getting an object property
Set represents a method of setting object properties
Example
var book = {
_year:2004,
edition:1
};
Object.defineproperty (book, "Year", {
get:function () {
console.log (' Get Year ');
Return This._year
},
set:function (newvalue) {
console.log (' Set year ');
if (NewValue >) {
this._year = newvalue;
This.edition + = newValue-2004;
}}
);
book.year = 2005;//console output ' Set year '
Console.log (book.year);//console output ' Get year ' and year's value
Object methods
ToString converts an object to a string, and the default conversion is something like [object], so you need to turn to JSON format to use the Json.stringify
valueof need to convert an object to another type. Similarly, the default conversion is nothing to say.
Executable Object
You can create an executable object by using the following methods
function Bar (o) {
var f = function () {return "Hello world!";}
o.__proto__ = f.__proto__;
f.__proto__ = O;
return f;
}
var o = {X:5};
var foo = bar (o);
Console.log (foo ());
Console.log (foo.x);
Console.log (typeof foo);//function
It can be used as an object (with a prototype chain) or as a function to directly invoke the