JS Object Details

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.