Is it better to do this...
var obj1 = {
a: function(){...}
}
var obj2 = Object.assign(Object.create(obj1),{
b: function(){ this.a(); }
});
obj2.b();
or...
var obj1 = {
a: function(){...}
}
var obj2 = {
b: function(){ obj1.a(); }
});
obj2.b();
I'm building a webapp mostly with the first pattern, but I'm starting to question if the performance impact of using 'this' is really worth the extra step.
Sorry if this is dumb or been posted before, I'm still trying to wrap my head around the idea of prototypal inheritance and it's all really confusing
As noted in the comments, your syntax is wrong, so neither will work.
But, your question really is simply "Is inheritance better than not using inheritance". The fact that JS uses prototypical inheritance really isn't the issue.
And, the answer is going to be based on opinions, so here's mine.
Inheritance is better because the code becomes more flexible and less brittle. Brittle code is always something to be avoided in software designs. Does it add more complexity to the code? Sure, but don't confuse complexity with readability. It seems that many developers shy away from more complex code in the name of "this is harder to read and follow", when that shouldn't be a litmus test for your code in the first place. Code patterns do often add additional layers of complexity and redirection to the code base, but what you gain is a more flexible code base that can adapt to change.
The use of this
isn't going to affect performance at all because this
is an automatically assigned object reference that will happen whether you use it or not. And, it's just a copy of a memory address, not a copy of the object itself.
From a performance standpoint, given the optimizations that modern user-agents employ, you are unlikely to notice any appreciable difference, but the inheritance mode is, by its very nature, going to require more from the runtime as the prototype-chain becomes a factor.