On Fri, Jan 15, 2021 at 1:48 AM Ron Prestenback
<
ronpres...@gmail.com> wrote:
>
> Hi Ben, thanks for the reply! I have worked with C++ for many years but this is my first exposure to javascript. I'm using the web editor for replying to this, so apologies if the formatting gets messed up :) I'll try to do inline quotes as well but not sure how that will turn out, so I'll try to space them out to help readability.
>
> I have read over the doc on prototypal inheritance here:
https://javascript.info/prototype-inheritance and
https://javascript.info/function-prototype I assume that covers all the important stuff?
>
>> - Constructors return an object
>> - The prototype template is used to configure that object's prototype
>
>
> Why wouldn't the object's prototype be the instance template? The object returned by the constructor is an instance, right? And the object's prototype would the "template" the instance was configured from, no? I'm starting to suspect that "prototype" and "template" have completely different meanings in v8, whereas in standard usage these terms are synonyms. This is where I get confused, I guess....
"Prototype" has a very specific meaning in JS - it's the chain of
objects that are searched to resolve properties that don't exist in
the instance.
Example: var x = o.x;
If o looks like { x: 42 }, then o.x is an instance property, but
otherwise the prototype of o is searched, then the prototoype of the
prototype, and so on. To illustrate:
var A = { x: 42 };
var B = { __proto__: A };
var o = { __proto__: B, y: 1337 };
var x = o.x; // Searches the chain until it finds A.x
var y = o.y; // But this lives in the instance
"Template" is just V8 parlance. The template describes the "shape" of
the prototype or instance object.
One way to think about templates is that they're an optimization that
lets V8 allocate the needed space for an object (or prototype)
upfront.
>
>> > What is the difference between a prototype template and an instance template?
>> It's the difference between `this` and `this.__proto__`, or:
>> function F() {
>> this.x = 42;
>> }
>> F.prototype.y = 1337;
>
>
> Sorry I'm really trying but I'm not sure how to apply your answer to my question. In your example, F.prototype would be the same as the __proto__ object for all instances of 'F', right?
Correct.
> If I'm understanding correctly (which is doubtful), in v8 parlance that makes F.prototype the instance template of F, right? If not, which thing is the instance template?
The body of F (the line that sets `this.x = 42`) corresponds with the
instance template.
>> > If I have a constructor function that I want to run when the new objects of the class are created, where should that go?
>> FunctionTemplate. Methods are set with FunctionTemplate::PrototypeTemplate().
>> (Technically you can also set them on the instance template but that's
>> kind of pointless.)
>
>
> Oh, why would it be pointless to set a method on an InstanceTemplate?
Because you use the InstanceTemplate for values that are likely
different between different instances of the same class (think: a
Point class with x and y coordinates).
Methods on the other hand don't normally change so those belong on the
prototype.
>> > Depending on the answers to the previous 3, this question may be moot, but: Is there any reason to have the same function or property on two or more of these "entities"? FunctionTemplate and Function? Or Function and PrototypeTemplate? etc. For example, I have some existing code that is first setting a property (via Set) on the PrototypeTemplate of the FunctionTemplate, then it also sets that same property (with same value) on the FunctionTemplate. What might be the intent here?
>> Probably to make it available as both a method and a class method:
>> o.m() and F.m()
>
>
> OK, that makes sense. What would the difference be if I set that property on the InstanceTemplate instead? Or to put another way, why would I ever set something on an InstanceTemplate instead of a PrototypeTemplate?
See above. :-)