Basic understanding of Function prototype prototype and implicit prototype __prot0__ of Object

Source: Internet
Author: User

Prototype prototypes:

I. Relationship of functions to objects
1. A function is one of the objects (the function is the object type)
Example: function fn1 () {...}
Console.log (fn1 instanceof Object);
Returns true, stating that the function (FN1) is an object type.


2. The object is created by the function
Example: var obj = new Object ();
var arr = new Array (3);
Arr[0] = 0;
ARR[1] = 1;
ARR[2] = 2;
Console.log (typeof Object); Obj is created by object
Console.log (typeof Array); Arr is created by array
Return: The above two console.log () statements return function,
The description object is created by a function.

Two. Prototype prototypes (properties of functions)
1. Principle: Prototype is a function of the property, each function has prototype this property, the value of this property is an object, the object is the default only one constructor property, and this object is pointing to the function itself.

2. The difference between a property in a constructor and a property in a prototype
1. Properties (or methods) in the constructor
Example: function Fun () {
This.arr = [1,2,3,4,5];
}
var fun1 = new Fun ();
Fun1.arr.push (6);
var fun2 = new Fun ();
Console.log (Fun1.arr); return {1,2,3,4,5,6}
Console.log (Fun2.arr); return {1,2,3,4,5}
Description: A property in a constructor that is owned independently of the object created by the function, that is, arr in fun1, and arr in fun2 is two different array objects that occupy different memory regions.

2. Attributes (or methods) in the prototype
Example: function fun () {...}
Fun.prototype.arr = [1,2,3,4,5];
var fun1 = new Fun ();
Fun1.arr.push (6);
var fun2 = new Fun ();
Console.log (Fun1.arr); return {1,2,3,4,5,6}
Console.log (Fun2.arr); return {1,2,3,4,5,6}
Description: The property set in the prototype of the function, for objects created by the function, is shared by these objects, which always occupies only one area in memory, that is, the value of the property of any one object is modified, and the value of the property of the other object is modified because they share a piece of memory area

Overall Description: 1. As seen above, defining attributes in a prototype is smaller than defining the amount of memory consumed within the constructor (always occupying only one chunk of memory).
2. Properties defined within the constructor have precedence over the attributes defined in the prototype, and if a property or method of the same name is defined, the property or method within the constructor overrides the property or method of the same name in the prototype.
3. So, if there is no special case, we typically define the property in the function body, defining the behavior in the prototype.


Three. __proto__ implicit prototypes (Properties of objects)
1. Principle: __proto__ is the property of the object, each object has __proto__ this property, the object's property is pointing to the prototype of the function that created the object.
Example: function person () {}
var p1 = new Person ();
Console.log (p1.__proto__); Return person{} This function
Console.log (Person.prototype); Return person{} This function
Explanation: 1. (p1.__proto__) refers to the prototype of the person's function (Person.prototype), (person.prototype points to person {}, so (p1.__proto__) The result of the final return is the function of person{};
2. Person.prototype is pointing to person{} this function
Therefore, (p1.__proto) = = = (Person.prototype) This expression returns TRUE.

2. Various points (fun1 as a function)

1. fun1.prototype.__proto__----> Object.prototype

Explanation: 1. The prototype of a function is an object, so it has __proto__ property;
2. The prototype here are custom functions, and the custom function is essentially created by the object function;
3. object.prototype.__proto__----> null;

2. fun1.__proto__----> Function.prototype

Explanation: 1. The function is the object type, so it has __proto__ this property;
2. Functions are created by function, so fun1 is created by function, so create
FUN1 The function prototype of this function object is function.prototype;

Elicit: Object is a function, so object is created by function: object.__proto__----> Function.prototype;

3. function.__proto__----> Function.prototype

Explanation: 1. function is a function, which is also a kind of object, so functions have __proto__ property;
Functions are created by function, so function object is created by itself, so
The function prototype for creating function object is Function.prototype;

4. function.prototype.__proto__----> Object.prototype

Explanation: 1. The prototype of function is the object created by objects, so this prototype __proto__ points to Object.prototype;


Four. instanceof (judging the type of object)
1. typeof is used to determine the value type, string/boolean/number; when it determines the reference type, the return value is only object/function,
You don't know if it's an object, an array, etc. (not quite understood);

2. Instancof judgment rule: a instanceof B, find the prototype of the function that created object A along the __proto__ property of A;
Find the function of B along the prototype property of B; If two lines can find the same reference,
Or the same function object, returns True if the final point is found and is coincident, returning false.

Example: 1. Console.log (Object instanceof Function); Returns True
Explanation: The function prototype of object is Function,function's prototype pointing to function, so it returns true.

2. Console.log (Function instanceof Object); Returns true (not quite understood)
Explanation: The function prototype for Function,object is prototype to object, and the function is one of the objects, so it returns true.

3. Console.log (function instanceof function);//return True
Explanation: Function prototype for function,function prototype points to function, therefore returns true





-
-





Basic understanding of Function prototype prototype and implicit prototype __prot0__ of Object

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.