Prototype object

concept

1 We divide the objects in JS into ordinary objects and function objects.
2 Function objects have prototype properties – except Function.prototype – ordinary objects do not have this property.
3 All functions are instances of Function() constructors – Function.prototype = = fn / Function.prototype.prototype == ubdefined
4 The main function of prototype-all the properties and methods added to the prototype object will be shared by all instance objects-that is, inherited
5 objects have proto hidden properties-mainstream browsers come with Yes , not for developers.
6 objects have a constructor attribute – the attribute value is the constructor or class of the object.
7 root constructor Object – Object.prototype.hh = 777 --> all objects can access hh.
8 prototypes Chain-is the special access relationship between multiple prototypes
9 prototype== JS prototype object / prototype object that comes with the proto browser

Relationship between prototypes

1 理解
	1 obj.__proto__ == obj构造器的 prototype   -- 一个对象可访问它原型链上的所有属性和方法 
	2 构造器的 __proto__ == ƒ () {
    
     [native code] }
	3 Object == 根构造器
	4 Object.prototype == 原型链的末端     // 例 -- 所有对象可以使用 toString() 的原因就是, 该方法存在于原型链的末端
	5 Object.prototype.__proto__ == null     // 说明根构造器的原型对象没有构造器

2 常见场景
	1 创建字符串 -- var str = "zhang"
	2 原型指向 -->  str.__proto__ == String.prototype
	3 原型指向 -->  String.prototype.__proto__ === Object.prototype   //记住 String这类构造器的 __proto__ 就是直接指向原型链子末端的
	4 原型指向 -->  Object.prototype.__proto__ === null

4 案例解析
	1 Object.prototype.hh = 77   -->  var str = "zhang"  --> console.log(str.hh)  // 77
	2 浏览器通过原型链查找 hh 属性过程
		1 str.hasOwnProperty('abc')   false  
		2 str.__proto__.hasOwnProperty('abc')   false
		3 str.__proto__.__proto__.hasOwnProperty('abc')   true -> 返回 hh 的属性值 66
		4 str.__proto__.__proto__ == String.prototype.__proto__ == Object.prototype

inherit

1 Concept

Inheritance in JS is realized through prototype-the property method of prototype can be shared by prototype's own instance object prototype chain
.

2 Ways of inheritance

1 Constructor inheritance has limitations...
2 Prototype inheritance is unreasonable...
3 Copy inheritance of parent and child objects will be affected...
4 Commonly used inheritance methods for mixed inheritance
5 Commonly used inheritance methods for class inheritance

3 -- 混合继承的实现方式

Guess you like

Origin blog.csdn.net/weixin_46178697/article/details/112789755
Recommended