C/C ++ simulates the JavaScript prototype Mechanism
To gain a deeper understanding of the JavaScript prototype mechanism, we can use pseudo-code to implement it or simulate this mechanism. Maybe my understanding is incorrect. If so, I still hope to give it some advice.
I. Prototype Mechanism Design
In JavaScript, there are two objects with the same ancestor:Function. prototypeAndObject. prototype.
(1)Object. prototype: This object is the root of all objects. It has no parent, yesNvwa.
(2)Function. prototype: This object is the root of all constructors. In fact, this object does not have aunt.Nvwa. But its internal maintainersObject. prototype.
In fact, all objects are maintained with one{Prototype}Attribute, which is an internal private attribute and cannot be accessed through objects. However, firefox and other browsers provide a common property._ Proto __To access this private property. In Js, the Public and Private Property names can be the same. But it cannot be in C/C ++. We need to make some changes. And thisNvwaTwo ambassadors were dispatched respectively.MetaObjectAndMetaConstructorThis is the class in C/C ++. They are responsible for generatingObject. prototypeAndFunction. prototype (in standalone Mode);
1. Object. prototype (1) General Attributes
1. Constructor: For functions that create objects (Constructor) Reference (pointer), here isObject, MainlyObject. prototypeAlso belongsObjectType. The design is not actually constructed by the Object.
2. Prototype: Private prototype attribute, that is_ Proto __Is null.
(2) general methods
1. hasOwnProperty (property): Determines whether an object has a specific attribute. This attribute must be specified with a string (for example,
O. hasOwnProperty ("name ")).
2. isPrototypeOf (object): Determines whether the object is a prototype of another object.
3. propertyIsEnumerable (property): Determine whether a given property can be used with... In statement.
4. toString ():Returns the original string representation of the object. The ECMA-262 does not define this value for the Object class, so different ecmascriept implementations have different values.
5. valueOf (): Returns the original value most suitable for this object. For many classes, the value returned by this method is the same as the return value of toString.
After all, JavaScript is a dynamic language, while C/C ++ is a static language. What we implement is a framework without any specific details.
2. Function. prototype (1) General Attributes
1. 0... n attributes:This is the index when arguments takes the parameter, 0 -- n
2. arguments attributes:Stores all parameters, which is equivalent to a variable parameter list.
3. callee attributes:Reference the current function object for function recursion, especially in anonymous functions.
4. caller attributes:Returns the function object that calls a function.
5. constructor attributes:Function. The constructor of prototype is Function.
6.Length attribute:When you create a function instance, the function length attribute is initialized by the script engine to the number of parameters in the function definition.
7. prototype attributes:Internal {Prototype} -- that is_ Proto __, Which is Object. prototype.
(2) general methods
1. apply method:This method allows you to use a function, as if the function is a function method of another object.
2. call method:It is the same as apply, except that the parameters are transmitted in different forms.
3. toString method:Converts an object to a string.
4. valueOf method:Check it by yourself.
We only provide the interface here and it is not implemented.
Ii. Data Structure 1.
MetaObject -- generate Object. prototype in Single Mode
MetaObject {public: static constructor = Object; // In fact, it is not constructed by the Object, but to make the Object. prototype belongs to the Object class static _ proto _ = {Prototype}; private: static {Prototype} = null; // assume that the private common variables do not conflict with public: // a bunch of interfaces}2.
MetaConstructor -- Function. prototype generated in standalone Mode
MetaConstructor {public: static constructor = Function; // It is not actually constructed by the Function, but for the Function. the prototype type is Function static prototype = undefinded; // It is not defined because it is not used as a constructor to use static _ proto _ = {Prototype}; private: static {Prototype} = Object. prototype; public: // a bunch of interfaces}3. Function: The base class of the Function constructor that you can use
Function{public: static constructor = Function; static prototype = Function.prototype; static __proto__ = {Prototype};private: static {Prototype} = Function.prototype;}4. Object -- Object base class Constructor
Object{public: static constructor = Function; static prototype = Object.prototype; static __proto__ = {Prototype};private: static {Prototype} = Function.prototype;}There is a unified formula: if there is an object obj and its Constructor is constructed, then:
Obj. _ proto _ = Constructor. prototype;
3. If the prototype chain has three classes, namely A, B, and C (in fact, the constructor-function object), for example, B inherits A and C inherits B, it is equivalent:
Function A () {}; //. prototype = Object. prototype,. _ proto _ = Function. prototypefunction B () {}; // analogy function C () {}; // analogy B. protype = new A (); C. prototype = new B (); var c = new C ();In c, a B instance is maintained, and A reference of A instance is maintained in B instance. It is equivalent:
function A(){};function B(){};function C(){};var a = new A();B.prototype = a;var b = new B();C.prototype = b;c = new C();That is to say, in c, the maintainer refers to instance B of instance B, while in B, the maintainer refers to instance A of instance a of instance A of instance. To put it bluntly, the inheritance here is actually a shared object, and it can form a shared chain. As shown in the following figure:
(A) Before inheritance:
function A(){};function B(){};function C(){};var a = new A();var b = new B();var c = new C();Structure
(B) After inheritance:
function A(){};function B(){};function C(){};var a = new A();B.prototype = a;var b = new B();C.prototype = b;c = new C();
Html (highly recommended)
|