Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of the JavaScript interface keyword

Tags:

javascript

First of all, no, I'm not trying to create any sort of Java-like interface for my JavaScript code. I've seen those questions all over, and while I'm still a relative novice to JavaScript, I know those aren't part of the language.

However, I'm curious what the actual intended use of the interface keyword is. For example, Math is an interface, containing definitions (but not implementations). I believe (and may be totally wrong) that these are there to provide a means for the definers of the language to enforce a set of behaviors to be implemented in various JavaScript engines. Is that correct?

Furthermore, I have a desire to have a "static class" that contains a bunch of utility methods. I like that Math.sqrt(3) has an outer namespace ('Math') which is capitalized, and a number of logically similar methods and values in it. Maybe it's just my Java/Ruby background that makes me want a capital on the grouping objects. Is that bad form?

var ShapeInspections = {
  isSymmetrical: function (s) {
    // determine if shape is symmetrical
  },
  numAngles: function (s) {
    // return the number of angles
  }
}

A purely contrived example, but is it anti-idiomatic to name the "module" this way?

like image 776
Eric Haynes Avatar asked Apr 13 '15 07:04

Eric Haynes


2 Answers

Okay, so as with other answers, you know that the keyword interface has no real use case in Javascript world, yet.

Your Math example made me suspicous that you are talking about a design pattern, called Module Pattern, widely used for scoping Javascript code. There are many ways of making your code modular. For example just like OddDev answered you , the famous Prototype Pattern can embed your code in a modular fashion (just like your Math example). Here is the Revealing Prototype Pattern example with also private variables and functions for additional flexibility:

/* Example from: 
    http://www.innoarchitech.com/scalable-maintainable-javascript-modules */
var myPrototypeModule = (function (){

   var privateVar = "Alex Castrounis",
       count = 0;

   function PrototypeModule(name){
    this.name = name;
   }

   function privateFunction() {
      console.log( "Name:" + privateVar );
      count++;
   }

   PrototypeModule.prototype.setName = function(strName){
      this.name = strName;
   };

   PrototypeModule.prototype.getName = function(){
      privateFunction();
   };

   return PrototypeModule;     
})();

but that is not all. Other options include Scoped module pattern, POJO module pattern and many more. Have a look at How to Write Highly Scalable and Maintainable JavaScript: Modules, it has a very simple and yet thorough set of examples.

So far, we talked about plain Javascript. If you have the ability to use libraries in your code, then amazing set of libraries such as Requirejs, CommonsJS are there to help you on this with out-of-the-box functionalities. Have a look at Addy Osmani's post about Writing Modular JavaScript With AMD, CommonJS & ES Harmony.

like image 119
ambodi Avatar answered Oct 27 '22 11:10

ambodi


The interface keyword in javascript is a FutureReservedWord, so it does absolutely nothing right now, though that may change in the future specifications. (See ECMAScript 5.1, section 7.6.1.2). In the ES6 draft, this is also the same.

As for you module, this is a perfectly idiomatic solution. It is always a good idea to "namespace" your functions, as it keeps the global scope as clean as possible.

like image 36
meskobalazs Avatar answered Oct 27 '22 11:10

meskobalazs