In JavaScript, Objects (Objects) are data (variables), attributes, and methods. Almost "Everything" JavaScript is regarded as an object. Date, array, String, function... You can also create your own objects in JavaScript.
Create object
• Direct object quantity
var o = { foo : "bar" }
• Constructor
var o = new Object();
• Prototype inheritance
var p = Object.create(o);
Class inheritance
Javascript objects have their own and inherited attributes.
• When querying the property x of object o, first look for the property x in o. If not, look for the x property in o's prototype object, until x or an object whose prototype is null is found.
• When assigning values to the x attribute of object o, if o already has a self-owned x attribute, the value of x is changed. If o does not have the attribute x, creates an x attribute for o and assigns a value to it.
• That is, the prototype chain takes effect only during query.
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 Attributes
• Traverse Object Attributes
You can use for .. in to traverse object attributes.
When you use for .. in, attributes on the prototype chain will be traversed. The traversal order is the breadth-first traversal.
So hasOwnProperty can be used to determine whether it is an object's own property.
• Features of Object Attributes
Use Object. getOwnPropertyDescriptor () to obtain the descriptor of a specific Object property.
Writable indicates whether object attributes can be written.
For example
Var o = {foo: 'bar'} Object. defineProperty (o, "foo", {writable: false}); o. foo = 'World'; console. log (o. foo); // still output bar
Enumerable indicates whether object attributes can be enumerated.
For example
The enumerable of attributes such as length in Array is false. Therefore,
for (p in Array) { console.log(p);}
No output
Configurability indicates whether the configurability and enumeration of attributes can be modified.
You can use Object. defineProperties to define these configuration attributes.
Object. defineProperty (o, "foo", {writable: false });
Get indicates how to obtain object attributes.
Set indicates how to Set object attributes.
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> 2004) {this. _ year = newValue; this. edition + = newValue-2004 ;}}); book. year = 2005; // The console outputs 'set year' console. log (book. year); // The console outputs 'get Year' and year values
Object Method
ToString converts an object to a string. The default conversion will be something like [Object object]. To convert it to json format, you can use JSON. stringify.
ValueOf must be used to convert an object to another type. Similarly, the default conversion is nothing worth mentioning.
Executable object
You can create an executable object using the following method:
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 the original type chain) or directly called as a function