What is the best javascript object template

What is the best Javascript object template ...

function dog(name) { this.name = name; this.getName = function() { return this.name; }; }; 

OR

 function cat(name) { this.name = name; }; cat.prototype.getName = function() { return this.name; }; 

AND WHY?

----- edits

Does this or that memory use more?

Is more or less โ€œCPUโ€ more intense than another?

Which is more convenient to maintain?

Which is more scalable?

Which is more readable?

+6
source share
5 answers

Preferences aside, the second example is โ€œcorrectโ€. In the first, you create a new getName function for each object. In 2, all objects created using this constructor will share the / getName prototype. Change it in one place and it will change for each instance.

In special cases (for example, complex inheritance chains), you may need to use the first, but keep in mind the disadvantages.

This blog post can help you better understand prototype inheritance.

+7
source

Just a personal opinion here ... but I prefer the dog style notation for long-term maintainability.

All dog codes remain in the dog definition. Imagine this after 5 years, and the โ€œnext guyโ€ will be asked to remove the hasTail property. No matter how many other objects / properties / etc. were defined between them and now, he will find it in the definition of a dog.

For comparison ... imagine this after 5 years, and the "next guy" will be asked to remove the hasTail property from "cat". It should scan the / grep / ctl + f animals.js file for hasTail and hopes it will remove the correct one.

+2
source

I personally prefer to wrap function definitions inside an object (e.g. dog designation), since it looks more like a Java / PHP class. However, using musical notation for dogs is less efficient in terms of memory, as functions are copied every time a new instance is created (see. Article)

+1
source

I use cat notation because it does not introduce unnecessary closures, which can be a little inefficient.

However, I have never tested how inefficient they are, and this may not greatly affect this age of hyper-optimized JavaScript engines.

0
source

I prefer the cat method, which uses a prototype.

  • I would prefer that all my methods do not depart from another level inside the constructor.
  • I would prefer the constructor code to be just the constructor code, and not many other functions - I think this makes it easier to see what you are looking for.
  • I would rather use a prototype, so all methods should not be assigned every time the constructor starts.
  • It also seems to me that this is why the whole prototype exists, so you can impose on it everything that future instances will be inherited automatically, without having to run code to assign them.
  • In addition, if you ever want to mix, you can get all the methods from the prototype without having to create one of the objects.
  • When you start subclassing, I find the prototype scheme more understandable.
  • Itโ€™s easier for me to look for method definitions because they all include .prototype. .
0
source

Source: https://habr.com/ru/post/895557/


All Articles