The difference between dynamic type language and static type language
Depending on the type of data, the static language has already determined the type of the variable at compile time, and the dynamic language is given a certain value after the variable is assigned to it at the time of the program's operation.
Why static language is more cumbersome than dynamic language in actual development
Static languages are type-tested at compile time, that is, functions can only be set to receive what type of variable. In order to achieve polymorphism, the possible values must be placed in a class, the function accepts the class, and the specific variable inherits the type of the class.
Dynamic language does not need to be so cumbersome, because he does not have strict type detection, but this will also bring some inexplicable problems. Each has its advantages and disadvantages.
What is polymorphic
Receive different parameters, execution results are not the same
The separation between "what to do" and "who to do" can also be understood as "what to Do" and "How to do" are separate.
The role of polymorphism
Digestion of the conditional branching statements, without the use of the IF statement detection type, directly invoke the behavior can be.
Packaging
In the narrow sense, it is the hidden variable, which makes the external inaccessible; In general, it hides all implementation details and provides only input and output interfaces.
The need for change is encapsulated, and the rest is reusable.
Prototype inheritance
The essence is to clone another object instead of instantiating a class (class inheritance).
Prototype chain
Finding an object as a prototype and cloning it, without methods and variables, will look up and delegate their own prototypes.
Object.getprototypeof
Object.getprototypeof (obj) can get the prototype of obj, and the obj.__proto__ is completely equivalent
1 console.log (object.getprototypeof (obj) ===obj.__proto__)// true
JS's prototype inheritance
The object created is implemented by the clone Object.prototype, that is, the clone is a prototype of an object, not the object itself.
But consider the memory allocation will find that the new object does not take up the same memory as the prototype, that is, the cloning of the substance is more similar to the reference, need to call this method will be called by the ancestors. So in order to improve performance, reduce the waste of memory, when adding common methods, the majority is added to the prototype (prototype), which can be well inherited by other objects.
Prototype and __proto__
JS is based on the prototype inheritance, that is, the real inheritance of things are in the prototype, if obj is an object, that Obj.prototype is also an object, we inherit or clone is the object of the properties and methods, here we call it the prototype object.
__proto__ is a property of all objects, a prototype object that points to its constructor, that is, a prototype to the constructor, or a good way to understand it is to point to an inherited object, that is, __proto__ points to the object of his own cloning, its own prototype, Here we refer to __proto__ as prototypes or prototype pointers.
At the core, the __proto__ of all objects point to the prototype of their constructors, that is, the prototype of all objects is the prototype object of their constructors.
Example:
function Person () {}; var p=New person ();
// The undefined constructor has the prototype object (prototype), and the common object Search prototype should be __proto__console.log (p.__proto__===person.prototype); // true the prototype (__proto__) of P (object) is the prototype object (prototype) of the person (constructor )
The constructor object has a property that can return its constructor
Console.log (P.constructor===person) //true console.log ( Person.prototype.constructor//True The prototype object is also an object and can point to its own constructor
So the following are all set up
// true
Console.log (p.__proto__.constructor===person.prototype.constructor); // true
Console.log (p.constructor===p.__proto__.constructor) // true itself and the constructor of the prototype is the same
So plainly, the inherited thing is the prototype object, and __PROTO__ is the prototype object that points to the inheritance .
So question one: Does the prototype object have a prototype object?
Console.log (Person.prototype.prototype) //undefined
Conclusion: The constructor has the prototype object, then the prototype object is no prototype object.
Problem two: The prototype object is also an object, does it have a prototype?
Console.log (person.prototype.__proto__===object.prototype) //true
Conclusion: Some prototypes (. __proto__) are also a prototype object
Question three: What is the archetype of all objects?
The prototype of the var a={};console.log (a.__proto__===object.prototype) //true object is Object.prototypeConsole.log (object.prototype.__proto__); // NULL
Conclusion: Object.prototype is at the top of the prototype chain and contains many methods, attributes, which can be understood as prototypes of all objects, and its prototype is null, which is empty and does not exist.
It is interesting that object is the constructor, which is both a constructor and an object, and the object has a prototype.
Console.log (object.__proto__===function.prototype); // true Prototype object for the prototype function of Object
// true while the prototype object of Object is the prototype of the function prototype object, which is a bit around, for the following notation
Console.log (object.prototype= = =object.__proto__.__proto__ ); // true to see that the prototype object of object is the prototype of its prototype
We have said that the prototype means cloning inheritance, that is, object around a circle to inherit its own prototype object, the prototype chain should be a chain, __proto__ connected to inherit the object, prototype to others, and object here a knot, this particularity shows its special status , and the key to "knot" is the function object, and another one that might be clearer
Console.log (function.constructor===function) //console.log (function.__proto__ // true
It all comes from here, function itself is its own constructor, so __proto__ (prototype) points to their own prototype (prototype object)
function is also the constructor of object
// true
So the __proto__ of the object points to the prototype of the function, so it's just sort of.
Object,
object.__proto__/function.__proto__ =>function.prototype,
function.prototype.__proto__ = Object.prototype,
object.prototype.__proto__ = null
Prototype mode
Design patterns that are inherited by cloning
JavaScript Design patterns and development Practices read notes (1)