javascript local properties, inherited properties, prototype chain

  1. Javascript has own properties , and some properties are inherited from prototype objects, called inherited properties .
    function Obj() {
          
          
    	this.x = 10;		// 本地属性 own property
    }
    Obj.prototype.x = 0;	// 继承属性
    Obj.prototype.y = 1;	// 继承属性
    
    let o = new Obj();
    console.log(o)
    
    The print result is as follows:
    Insert picture description here
  2. Prototype chain
    Suppose you want to query the attribute y of object o, if y does not exist in the local attribute, then it will continue to search for attribute y in the prototype object of o. If there is no y in the prototype object, but the prototype object also has a prototype, then continue to perform the query on the prototype of the prototype object until y is not found or an object whose prototype is null is found. The prototype properties of the object form a "chain". The inheritance of attributes can be achieved through this "chain".
    • Find inherited properties on the prototype chain console.log(o.y) // 1
    • Local attributes will block inherited attributes with the same nameconsole.log(o.x) // 10
    • When adding an attribute of a basic data type, if it has the same name as the inherited attribute, a new local attribute independent of the inherited attribute will be automatically created o.y = 20; console.log(Obj.prototype.y) // 1
    • If the inherited attribute is a reference data type, modifying its value will affect the attribute on the prototype
    Obj.prototype.list = [1,2,3];
    o.list[0] = 0;
    console.log(Obj.prototype.list); // [0, 2, 3]
    
  3. Three methods of attribute detection
  4. Three methods of property traversal

How to better understand the own properties and prototypal inheritance properties of Javascript objects

Guess you like

Origin blog.csdn.net/SJ1551/article/details/109275513