@[toc]

Explicit stereotypes Implicit stereotypes

Each function has a Prototype property

  • If you write a function yourself and it defaults to an empty Object (the prototype), you might say it’s not in thereconstructorand__proto__Yeah? Well, look at the bottom.

 

  • If you look at the prototype array function provided by js, you can see that it already has a lot of methods that are not empty objects.

                     console.log(typeof Array.prototype)

                     console.log(Array.prototype)

Copy the code

The prototype object has a property called Constructor, which points to the function object.

Constructor for the prototype object in the figure below is equivalent to the fun function I declared. Therefore, it can be understood that constructor of the prototype object and this function are mutually referenced.

Add properties (usually methods) to the stereotype, and all instances of the function automatically have the properties (methods) in the stereotype

The __proto__ object is the same as the prototype object in the function.

  1. Each function has an explicit prototype

  2. Each instance object has a __proto__, known as an implicit prototype

  3. Implicit stereotype of an object = explicit stereotype of its corresponding constructor

 

Graphic prototype (this is not complete, must read on to fully understand)

When you create a function, the diagram looks like this: the Fun function name is stored on the stack, and there will be an object in the heap that stores Fun. There is a Prototype property, which is an empty object. The function’s explicit prototype is automatically added when the function is defined. The default value is an empty Object

Create a new instance of Fun, fun1, that points to the same address. The implicit __proto__ prototype of an object is automatically added when the object is created, and defaults to the value of the constructor’s prototype property.

Add a method to the Fun prototype object that fun1 can also call. Programmers can directly manipulate explicit stereotypes, but not implicit stereotypes (pre-ES6)

 

Graphic prototype chain

To access a property of an object, first look in its own property, find the return. If not, look up the __proto__ chain again and find the return. If it is not found, return undefined

Alias: Implicit prototype chain

Function: Find the properties (methods) of an object

Blue line: Fun is an instance of fun. When fun calls printhaha and printaaa, it first looks for itself. If not, it follows the implicit prototype (fun.__proto__) to find fun’s display prototype (fun.prototype). Fun’s explicit prototype has printhaha and printaaa, calling execution.

Orange line: Fun is an instance of fun. When fun calls printhaha and printaaa, it looks at itself first. If not, it follows the implicit prototype (fun.__proto__) to find fun’s display prototype (fun.prototype). Not in the explicit prototype of Fun, continue to follow the implicit prototype of Fun’s explicit prototype (fun.prototype.__proto__), and finally find it in the explicit prototype of Object, call execution.

Fun is an instance of fun. When fun calls printbbb, it first looks for itself. If not, it follows the implicit stereotype (fun.__proto__) to find fun’s display prototype (fun.prototype). If there is no explicit prototype for Fun, then follow the implicit prototype of Fun’s explicit prototype (fun.prototype.__proto__) to find the explicit prototype of Object (object.prototype). If Object’s prototype chain points to null (object.prototype. __proto__===null), there is no point in looking down. If the lookup fails, the method cannot be called.

 

conclusion

Say first object

The implicit stereotype of the instance object points to the explicit stereotype of the function. Therefore, the implicit stereotype of all instances of object is the same, equal to the explicit stereotype of object

Besides, the function

When you create a function, you mostly use the first method, but you can also declare a function using the second method. If you create a new Function, it can also be considered an instance of Function.

The implicit stereotype of all functions refers to Function. Because every Function you define is an instance of Function.

  • Implicit stereotype of the instance = explicit stereotype of the constructor that is, the implicit stereotype of all functions points to the explicit stereotype of Function, so the implicit stereotype of all functions is equal.

  • Add another word: the stereotype object has a property called constructor, which refers to the function itself.

 

As you can see from the diagram, only the explicit stereotype of Function() is its own implicit stereotype. Function() is an instance of itself.

So all functions are instances of Function(), including itself

Function = Function; Object = Function; Function = Function; Object = Function; Since the instance’s implicit stereotype = the constructor’s explicit stereotype, it follows that their explicit and implicit stereotypes are indeed the same.

But the most amazing thing is that both of these statements indicate that the explicit stereotype of Function and the implicit stereotype point to Object. Function Object() ‘s __proto__ refers to the function Object()’ s prototype. See supplement for the most complete diagram.

Supplement:

  1. All functions are instances of Function(), including itself

  1. The prototype Object for Object() is the end of the prototype chain. Below we can see that the implicit stereotype of Object is null. So this is where the prototype chain ends.

Combined with the above summary of the function, we can put the picture to improve, this is the ultimate perfect version!!

  1. An instance of Object() is not an Object. The default Object is an empty instance of Object().

  1. When an object’s property value is read: it is automatically looked up in the prototype chain; When setting the property value of an object: the prototype chain is not looked up. If the property does not exist in the current object, the property is added and its value is set. Thus, methods are typically defined in stereotypes, and properties are typically defined on the object itself via constructors.

Here’s an illustration of ow:

Customize a function that sets the a attribute to the stereotype. Create two instances of Fun. The output of their a is aaa, but after changing a by f2, only f2 output will change.

If you look at this graph, you can understand it. If you don’t have it when you look for it, you go down the prototype chain. But when you set a value, you only set it to its own property, not to the prototype chain. So methods are typically defined in stereotypes, and properties are typically defined on the object itself via constructors.

  1. instanceofWhat is the

A instanceof B: return true if the explicit stereotype of B is on the stereotype chain of A, false otherwise.

You can compare that with my ultimate map above.

– Example 1: This is easy to understand, f1 prototype chain is Fun, Fun is Object, Object is empty, so the end of Object.

      

Example 2: the stereotype chain of Function points to the explicit stereotype of Object, and the stereotype chain of Object points to the null value. So it can’t be Fun.

        

– Example 3: Compare it to my final map above

        

 


I am an an, I this afternoon I draw pictures of super tired!!