zuloosale.blogg.se

Javascript constructor
Javascript constructor












javascript constructor

Accordingly, as you may know, any access to this before the super call results in an error. Long story short, it calls Construct with the SuperConstructor and current newTarget, and binds the result as this. The important part here happens in the super call. In that case, ] is not "base" (see step 15 of ClassDefinitionEvaluation), so in ], step 5.a does not apply anymore, nor is Ordinar圜allBindThis called. The confusion probably stems from the case, where extends is being used. The prototype is obviously set there aswell. This is the same for class and the ES5 way. Generally, it's Object.create(newTarget.prototype). For new f, it is basically Object.create(f.prototype). Note that Ordinar圜allEvaluateBody comes at a later step - the object is created, before the constructor function is evaluated. In the standard scenario (no extends), in step 5.a, ] calls Ordinar圜reateFromConstructor, and the return of that will be used as this (see Ordinar圜allBindThis, where it is used as argument). Proxies having custom behavior for it, as that is imho not related to the topic. I will only discuss the normal ] here, and not care about e.g. New will call Construct, which in turn will call the related function's internal ]. Note 2: With "created" I mean space allocated in memory and type set (exotic vs ordinary), at a minimum. I have been searching around a lot and have been unable to find the right algorithm-steps. Note 1: It would be great if the answer could include links to where in the ECMAScript specification each of the two creations occur. In both cases, when is the prototype property set? Is it before or after executing the constructor function / ClassBody? Therefore, I was thinking perhaps the new object is created right when the engine encounters the extends keyword and can read what type the parent is. Similarly to above, but for classes: When is the new object created?Ī guess: Since we can subclass built-in objects with class syntax, I am thinking the engine must know what type ( exotic vs ordinary) its parent object is. When creating a new object via a good old ES5 constructor function: When is the new object created?Ī guess: Is it created immediately when the JS engine encounters the new keyword, directly before the constructor function is executed?














Javascript constructor