1

What is encapsulation in context of JavaScript? I'm confused after reading this statement in mozilla web-site(link):

Encapsulation

In the previous example, Student does not need to know how the Person class's walk() method is implemented, but still can use that method; the Student class doesn't need to explicitly define that method unless we want to change it. This is called encapsulation, by which every class inherits the methods of its parent and only needs to define things it wishes to change.

I've understood encapsulation as hiding class members, but in the example on the Mozilla site it seems to be simple inheritance.

3
  • 2
    This is a fair question. The referred text does not clearly explain encapsulation, and seems to conflate it (incorrectly, IMHO) in the last sentence with inheritance. Commented Aug 28, 2013 at 17:10
  • It means the same in the context of JavaScript as it does in any programming language: en.wikipedia.org/wiki/… , Commented Aug 28, 2013 at 17:19
  • Encapsulation is not unique to class members. In here, encapsulation of behaviour (of the method implementation) is meant - but you're right, the phrasing of the last sentence is confusing. Commented Aug 28, 2013 at 17:26

2 Answers 2

1

It means that you don't have to be able to build the tools that you're using to use them.

It's makes programming a lot less stressful when you can abstract things like that away.


Have you ever used the alert() method in JavaScript?

I'm sure that you'd feel a bit overwhelmed if you had to care about how alert communicates with your browser, and how your browser communicates with your display and all the layers in-between.

You don't want to worry about the bezier curves used to render your fonts or how to implement the ok button, or all the other code that makes alert work. All you know is that you can write alert("txt") in JavaScript, and that a dialog box will appear.

Sign up to request clarification or add additional context in comments.

Comments

0

walk is implemented in Person. Student isn't allowed to change how it's implemented, it can only override the function completely.


You could design a programming language that allows you to override parts of the parent function rather than the function as whole. This programming language has inheritance but not encapsulation.

Now of course if a child overrides part of a parent function, this means the child and parent implementations are coupled. This is generally considered bad practice. This is why most languages go so far as to enforce encapsulation, but it's not something you absolutely need.


Maybe a good analogy is a plugin mechanism. You can write plugins in different ways: use some event hooking or use clever inheritance but you can also do inline code replacement. Now before you think this is ridiculous, older versions of the popular forum software phpBB actually did this. You can imagine what happens if you install two plugins that might interfere, there's no telling what will happen!

4 Comments

Encapsulation is not required for inheritance. You can have inheritance without encapsulation, but not having encapsulation violates a lot of good practices.
I might play with words here, however isn't allowed seems incorrect to me, since you could alway do this.walk.toString() to access how it was implemented. Doesn't have to know how seems more appropriate.
It depends on how far you extend encapsulation. You could design a programming language that has inheritance that allows you to override parts of the parent function. In this case you need more than toString. That fact that you don't need to is convenient.
@FritsvanCampen, I agree, however the target language is JavaScript here. There's nothing preventing you from performing some code analysis of the parent function to determine how it was implemented. I am not saying that you should, but you can. That's why I'm saying that Student isn't allowed to see how it's implemented isin't quite right. However, Student doesn't have to know, or shouldn't have to know how is more precise in my opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.