Pseudoclassical instantiation

We’ve recently been introduced to new pattern and I feel like I should be absorbing this material more easily, but I’m not. So, when you don’t know something, write about it publicly so that you have to make sure you know what you’re talking about.

This pattern attaches methods directly on the objects prototype. Instead of assigning Object.create(Object.prototype) to a new variable, the object gets set to ‘this’ for property assignment and method creation. This gets done automatically by the interpreter by using the ‘new’ keyword.

The following line gets run right automatically before the ‘new’ keyword.

this = Object.create(Object.prototype);

and the following line gets run automatically right after the ‘new’ keyword.

return this;

Now the Objects properties are accessible to any new instance by using the keyword ‘this’ on the property. This is helpful because if you need to create many instances of an object, you just need to call the function with the ‘new’ keyword and define all the properties on the object that you want. In functional instantiation, you’d have to create a new function for each new object. That’s a lot of typing and a lot of memory being eaten up.

In summary, pseudoclassical instantiation brings with the advantages of code reuse, readability (as long as you understand ‘this’. that might be another blog post) and delegating to object prototype methods.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.