Replies inline, hope it's useful to you. I'll assume you're familiar with prototypical inheritance in JS but let me know if you need more details.
On Thu, Jan 14, 2021 at 9:42 PM Ron Prestenback <ronprestenb...@gmail.com> wrote: > > Hello everyone, > > I've read a lot of information about FunctionTemplate / Function / > PrototypeTemplate / InstanceTemplate, but some details are still not clicking > for me. I feel like maybe there is a gap in the documentation that I hope > the answers to these questions can help fill: > > (FWIW, I'm creating a two-way association between C++ and javascript objects) > > The documentation for FunctionTemplate.PrototypeTemplate says it's the > "template used to create the prototype object of the function created by this > template". But what is the role of a "prototype object" for a function? I > understand the concept of a "prototype" for an object, but not a function. > From what I understand, the new object will already inherit all > properties/methods defined on the Function and FunctionTemplate, so what's > the prototype object needed for? And if there's only one prototype object, > why do we need a template for it? - JS functions can be invoked as constructors with the `new` keyword - Constructors return an object - The prototype template is used to configure that object's prototype > 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; > What is the difference if I put a property on the FunctionTemplate, the > Function, the PrototypeTemplate or the InstanceTemplate? What should I be > taking into account when deciding where to put the property? Setting properties on the first two is like setting properties on the JS function object: function f() {} f.x = 42; The last two are for construct calls: new F() > Should I use the same logic for functions/methods as well? Or are there > different considerations for functions? I don't understand the question, I'm afraid. > 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.) > 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() > I've seen code that calls RemovePrototype on the FunctionTemplate (or maybe > the Function....I'm not sure). For what reason would you do this? For the same reason you create prototype-less objects with `{ __proto__: null }` or `Object.create(null)` - usually to avoid prototype pollution / being able to use objects as plain dictionaries. HTH! -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/CAHQurc_z2XAKDhCxevHBURhKt8u0FaBeakqDApFpdF%3DBCgu3Tg%40mail.gmail.com.