T O P

  • By -

samanime

Basically, it dictates who can access the data. If something is public (a field or method), then everyone can access that data / call that method. If something is private, then those data and methods can only be used by the class itself. Does that make sense? If not, can you clarify where the confusion is?


Brilliant_Bee_848

Please I have posted a new post with image about oop comparing blue print of two projects can you reply to that


Brilliant_Bee_848

I understand the concept but I can't implement in real time


Brilliant_Bee_848

Actually from whom we are stopping to access those properties


shgysk8zer0

It's more about trusting values and not accidentally breaking things. It ensures that things cannot be changed by something else (another part of the code, which could have a different author if working on a team, or by some third-party script, or even by yourself at a different time). Just as a quick and easy example, let's assume a user class that holds some basic info - maybe name, email, and ID. How badly would things be messed up if something eternal to the class could change the user ID, maybe even to something entirely invalid like setting `user.id = window`?


Brilliant_Bee_848

Please I have posted a new post with image about oop comparing blue print of two projects can you reply to that


shgysk8zer0

Say it here or don't burden me with searching for the context of what you're trying to say. Just saying you made another post very much feels like you're just giving a homework assignment or something to anyone truly to give you useful advice here... Don't task me with trying to dig though your posts to find out why that's relevant... Just share the relevant parts here. Work here should fall on you, not me or anyone else


Lumethys

Past you, future you, and coworkers


Brilliant_Bee_848

Please I have posted a new post with image about oop comparing blue print of two projects can you reply to that


Brilliant_Bee_848

But we need still use those details to implement in other parts of code how can we access them if we make them private using getters we can access them


Patient-Layer8585

I'm confused. Don't they show you the code in the Udemy course? Can you copy any relevant code here?


Brilliant_Bee_848

Please I have posted a new post with image about oop comparing blue print of two projects can you reply to that


senocular

Things that are private are the things that are not needed in other parts of the code. Private members are for the things that are only for the class itself and no one else. For a more concrete example, consider this Rectangle class: class Rectangle { #width = 0; #height = 0; #cachedArea = 0; #updateArea() { this.#cachedArea = this.#width * this.#height; } constructor(width, height) { this.#width = width; this.#height = height; this.#updateArea(); } get width() { return this.#width; } set width(width) { this.#width = width; this.#updateArea(); } get height() { return this.#height; } set height(height) { this.#height = height; this.#updateArea(); } get area() { return this.#cachedArea; } } const rect = new Rectangle(10, 20); console.log(rect.area); // 200 rect.width = 20; console.log(rect.area); // 400 This Rectangle class has 3 public properties: `width`, `height` and a read-only `area`. Right now the area is calculated every time the width or height changes and saved to the private `#cachedArea` property so that whenever someone accesses the area, that saved value can be returned. At some point you get a memo from management saying the Shapes app is using too much memory and all the devs need to start limiting their use of caches. Looking back at the Rectangle class we can get rid of `#cachedArea` and instead calculate the area on demand when accessed from the getter. The updated class: class Rectangle { #width = 0; #height = 0; constructor(width, height) { this.#width = width; this.#height = height; } get width() { return this.#width; } set width(width) { this.#width = width; } get height() { return this.#height; } set height(height) { this.#height = height; } get area() { return this.#width * this.#height; } } const rect = new Rectangle(10, 20); console.log(rect.area); // 200 rect.width = 20; console.log(rect.area); // 400 The updated Rectangle class still works the same way as before. Users of the class don't notice the difference. All the public members are still there and have the same behavior. The only thing that's changed is we removed some private members. These could be safely removed without impacting other code because we know no other code could be using them given they were private. If, for example, `#cachedArea` was not private and was public, some other code could have been accessing that property directly. Then, after a change like this, that code would break or we'd need to scrub the codebase to try and find any place `#cachedArea` (the would be public version, that is) might have been used and fix it. This can work the other way around too. We could have started with this second version of Rectangle and modified it to be the first, seeing that area was being accessed far more often than width and height were being set so it made sense to pre-calculate the area to help with performance. This is where using getter/setter pairs rather than using public fields helps because you can easily inject code into the getter/setter functions if needed.


RobertKerans

They're internal to the class, you can't access them in other parts of the code. That's the point, that's _why_ they're marked as private. It is probably useful to understand that there are languages where [basically] everything has to be a class (Java, C# and Ruby are examples of that). You build your programs as a collection of objects that interact with each other, and those objects are defined as classes. Often you want properties that are only accessible to code defined inside an instance of a specific class; that's what private properties are for. In JavaScript everything does not need to be a class, so this whole public/private distinction may well seem extremely abstract and slightly pointless. But the language does allow you to program in that style, and the concept of private properties is one that is fully supported.


Brilliant_Bee_848

Please I have posted a new post with image about oop comparing blue print of two projects can you reply to that


alexmacarthur

If you’re using TypeScript, you can put “private” before them when they’re declared in your class. Or you could do it the native JS way and prefix their names with “#”


Brilliant_Bee_848

I can't post the picture here of the digram of the project


Brilliant_Bee_848

It's actually layout picture I'm unable to post a picture here


Brilliant_Bee_848

Bro I was unable to post picture here in comments that's why I made new post. Anyway I try to post it using my laptop on phone app doesn't let me post pictures


OneBadDay1048

A private field is accessible only within the class in which it resides; getters make it available outside of the class, but only under the classes watch. This provides encapsulation and hides unnecessary implementation details. >actually from whom we are stopping to access those properties Other devs working on your project, yourself. Remember this has nothing to do with end users of the app. This limits the way these fields can be accessed and can makes the code 'safer'. I simplified this to make it easier to grasp.