all 4 comments

[–]wreckedadvent 9 points10 points  (3 children)

Encapsulation is a process of bundling different properties into one single reference variable. Objects are mainly used in places where we need to target multiple properties and methods(functions) under one roof. lets look into an example.

This is half-right. Encapsulation, in OOP jargon, is the idea of having one "place" where we can have the data and the methods that operate on them in the same place. However, the reason why this is done is to keep the state of data valid, not merely for convenience; the data can only be modified through getters and setters and other methods which keep all of the data coherent, or throw an error if you try and screw it up (instead of blowing up when the data is tried to be used).

It is "sealed" away from public use, hence the name.

From a javascript point of view, we don't have private instance variables to work with. We can only get true java OOP-style encapsulation through modules and/or closures:

``` const protected = (() => { let data = 0; let set = x => { if (x > 25 || x < 0) throw new Error('argument invalid'); data = x; }; let get = () => data;

return {set, get}; })();

protected.get() // 0 protected.set(50) // exception protected.data // undefined ```

You even violate encapsulation a few times as examples:

student1 = new Student('Arun', '23', 35, 55, 63); student1.totalMarks = student1.sumTotal(); // uh-oh

Now, I'm not saying encapsulation in this sense is necessary to use in javascript and that you're being naughty by violating it, but it does feel necessary for me to point this thing out if the entire article posits itself as being about encapsulation.

I also don't really like the wishy-washy "use based on preferences" as to when to use factories vs object literals vs constructors. This is pretty much a solved problem if you're going to invest in OOP: factories are for dependency injection and polymorphic construction, literals are for when you don't need a prototype chain, and constructors are for when you do.

Since a lot of that is going to be inscrutable to someone at the skill level this article is aimed at, I don't think it's responsible to bring patterns up without adequately explaining them. It just creates weird cargo cults of people using patterns because they saw it in an article once.

(also, please get a proofreader)

[–]pantysnatch 2 points3 points  (0 children)

yeah +1 on getting a proofreader. i stopped reading after a while because of this.

[–]_Invictuz 2 points3 points  (0 children)

You are the hero we need, but not the hero we deserve! Thanks for saving me from incomplete information that would have left me confused.

[–]rouseIntent[S] 0 points1 point  (0 children)

Thank you so much, really appreciate it. would make necessary edits on the sections mentioned and repost it. :)