What’s this code for?

Asked

Viewed 102 times

5

I have seen this structure lately, in javascript, tried to search about it but could not get concrete answers. I know it is an Object -I think- but it is that I am beginner and did 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(){}
};

2 answers

9


This is the declaration of an empty object:

var PROD = {};

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

About the code:

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

Here we are declaring an object with two members, both of which are functions. Usually this kind of statement happens when you want to implement callbacks, that is, functions that will be defined later in other parts of the code.

The above definition is the class builder clients.

  • Is it practically an object orientation? Say this is a model with only the functions? If you wanted you could put an attribute and this method1 would inform a value in it, as if it were a set?

  • Yes, that’s right. You don’t need to define all the members of the object as functions. You can set as variables.

4

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

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

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

init: Function(){} should be implemented in this function the module construction.

method1: Function(){} should be implemented the method for this module.

  • 2

    Just be careful with the terminology. The terms class, module and method are not native to Javascript, although they are sometimes used analogously.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.