What is this code for?

5

I've seen this structure lately, in the javascript, I've tried to search for it but I have not been able to find concrete answers. I know it's an object, I think, but I'm a beginner and I do not understand how it works. Mainly the beginning -var PROD {}; ??.

var PROD = {};
PROD.clients = {
  init: function(){},
  method1: function(){}
};
PROD.pages = {
  init: function(){},
  method1: function(){}
};
PROD.accounts = {
  init: function(){},
  method1: function(){}
};
    
asked by anonymous 28.10.2015 / 22:30

2 answers

10

This is the declaration of an empty object:

var PROD = {};

The others are also, but as properties of this object as new objects.

As for the code:

PROD.clients = {
  init: function(){},
  method1: function(){}
};

Here we are declaring a two-member object, both of which are functions. Usually this type of statement happens when you want to implement callbacks , that is, functions that will be defined later in other snippets of code.

The above definition is the constructor of class clients . .

    
28.10.2015 / 22:33
4

This code is just a template, a code skeleton with no implementation in it.

The beginning var PROD = {}; is declaring an empty class.

And in the next lines adding the modules for this class.

Init: function () {} The module construction should be implemented in this function.

method1: function () {} method must be implemented for this module.

    
28.10.2015 / 22:50