Prototypes and prototype chains
In the object itself can not find the specified attribute, will be found on the prototype of this object, the prototype is also pointing to an object, the object can not find the corresponding property, then continue to the prototype to find ... The above process forms a prototype chain.
To access the prototype of the object: obj.__proto__ or can call object.getprototypeof (obj), the returned value is also obj.__proto__;
Obj.__proto__ and Obj.constructor.prototype point to the same object by default, which can be understood by default: obj.__proto__ = Obj.constructor.prototype = XX
The constructor for the empty object {} is Object
Adding or modifying properties directly on the prototype of an empty object will work on all of JS's objects, because all of the JS object's prototype chain ends are the objects that Object.prototype points to:
Object.getprototypeof ({}). x=13; Console.log ([].x) //
Object Properties
The properties added in the following ways are added to the object.
var= { x:123, method () { return 123 =; // function Person () {// THIS.A = 123; // }//// obj = new person (); Console.log (obj)
This includes object.assign (TARGET,SRC1,SRC2), which simply adds the properties of the SRC object to target. You can use assign to make shallow copies of objects.
Object traversal: http://es6.ruanyifeng.com/#docs The enumerable and traversal of the/object# property
For ES6 class, the method is defined directly on the prototype
class Point { toString () { // ... }}// equals = { toString () {},}; typeof // "function"
Cloning
First copy the prototype, then copy the properties on the object.
function Clone (Origin) { = object.getprototypeof (origin); return object.assign (Object.create (Originproto), origin);}
Ps:
The object.create (target) function actually creates and returns an empty object with Target as the prototype (__PROTO__).
Inherited
ES5 need to manually modify the prototype chain to implement inheritance
ES6 uses extend syntax to implement inheritance, essentially creating a parent class object (the constructor of the parent class must be explicitly or implicitly called in the constructor of the subclass), and then the object of the parent class is prototyped (__proto__), and the subclass object is created.
The constructor of the object through class new points to class, and in ES5 is similar to the constructor point function of the object through function new
JS Object Details