3

I am working on this web application that already has a mechanism that allows a developer to define server side JavaScript functions that can then be called by other server side JS code.

There are two ways to define these functions.

This way...

function iAmFunctionA(arg) {
   ...some code...
}

function iAmFunctionB(arg) {
   ...some other code...
}

...and by an option that encapsulates the functions inside a separate object (I think this is because the web application has Prototype.js embedded)...

var iAmAClass = Class.create();
   iAmAClass.prototype = {
      iAmFunctionA: function(arg) {
         ...some code...
      },

      iAmFunctionB: function(arg) {
         ...some other code...
      },

   type: 'iAmAClass'
}

This latter type is then called by other server side JS like so:

var a = new iAmAClass();
var someRetVal = iAmAClass.iAmFunctionA(arg);

The question I wanted to ask is: are there any practical benefit(s) from encapsulating JavaScript inside an object, and if so, what would that be?

  • I've never seen this JavaScript syntax before: iAmFunctionA: function{arg} { ...some code... } Is that legal JavaScript? – GlenPeterson Dec 2 '15 at 16:31
  • sorry @GlenPeterson I need to get my eyes checked;-P I had went a bit crazy on the curly brackets. Now the code is corrected. The proper format is to use parenthesis in the iAmFunctionA: function(arg)... – x457812 Dec 2 '15 at 17:17
3

I think this is really trying to put Object Oriented thinking into JavaScript. Your code looks just like this example: Create an Object-oriented JavaScript Class Constructor from HTML Goodies.

Object Oriented Programming is an organizational technique. If you have a User data structure, it might have fields like:

firstName
lastName
age

You might have functions that work with Users like,

createUser()
deleteUser()
changeFirstName()
addUserToCompany()

You could just throw all these things around willy nilly, or you could put all the user-related data and functions in one place - a User class. Maybe that last addUserToCompany() function belongs more on the Company class than the User class. It's not a perfect system. But you shouldn't have to look for that function anywhere but the User and Company classes, so at least it narrows down where you'd sensibly look for things.

Encapsulation usually means (roughly) "hiding." You hide things to limit the ways they can be used. If something has no moving parts, you're less likely to break it. So you hide all the sensitive stuff - hopefully more effectively than you would by adding a comment like // Don't touch this!

Unfortunately, everything in JavaScript is global, visible, and mutable. The only way to encapsulate things in JavaScript is to put them inside functions. You con't get most of the guarantees/benefits of OOP in JavaScript. But some people like OOP. Some problems are best solved with OOP, even if using JavaScript this way is a bit artificial.

What you are seeing here is someone setting up data and functions inside a closure to make the details of those things less public. I'm not sure how effective it is, since JavaScript objects provide almost no guarantees.

Most likely, the benefit in this case is that the person who wrote the code gets to think about code the way they are used to thinking in an Object Oriented language like Java. If you like to think that way, then it helps you. If not, then I don't see the benefit of trying to do OOP in JavaScript either.

But if your question is specifically about encapsulation, then as Robert Harvey mentioned, that's almost always a good thing. And putting things inside functions (or "closures") is the only way to encapsulate in JavaScript (at least until ES6).

  • 2
    But using JavaScript this way is a bit artificial. -- Javascript is a Prototype language. Classes aren't going to look like Java in Javascript (unless you're using ES6), but encapsulation is still a good thing. – Robert Harvey Dec 2 '15 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.