Create an object
•Direct Object Measurement
var o = { foo : "bar" }•Constructor function
var o = new Object();
•Prototype inheritance
var p = Object.create(o);
Class inheritance
Javascript objects have their own properties and inherited properties.
• When querying the attribute x of object o, first look for the attribute x in o. If it is not found, look for the x attribute in the prototype object of o until x or an object whose prototype is null is found.
• When assigning a value to the x attribute of object o, if there is already a property x in o, change the value of x. If there is no property x in o, create an x attribute for o and assign a value.
•That is, the prototype chain will only work when querying.
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'));//falseYou can use in or hasOwnProperty to determine whether there are properties in the object.
Object properties
•Transfer through object properties
You can use for..in to traverse the properties of an object
When using for..in, iterates over the properties on the prototype chain. The traversal order is to traverse with breadth priority
Therefore, using hasOwnProperty can determine whether it is an object's own attribute.
•Properties of object properties
Use Object.getOwnPropertyDescriptor() to get the descriptor for the object-specific property
Writable means whether the object attribute is writable
For example
var o = { foo : 'bar'}Object.defineProperty(o, "foo", { writable : false });o.foo = 'world';console.log(o.foo);// Still output barEnumerable means whether the object attribute is enumerable
For example
The enumerable of the length and other attributes in Array is false, so
for (p in Array) { console.log(p);}Nothing output
Configurable means whether the configurability and enumeration of properties can be modified.
These configuration properties can be defined using Object.defineProperties.
Object.defineProperty(o, "foo", { writable : false });
Get means a method to obtain object properties
Set represents a method to set 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 > 2004) { this._year = newValue; this.edition += newValue - 2004; } }});book.year = 2005;//Console output 'set year'console.log(book.year);//Console outputs the values of 'get year' and yearObject method
toString converts objects into strings. The default conversion will be something like [object Object], so if you need to convert it to json format, you can use JSON.stringify
valueOf needs to be used when converting objects to other types. Similarly, there is nothing worth saying about the default conversion.
Executable object
You can create an executable object by 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);//functionIt can be used as an object (with a prototype chain) or as a function to call it directly