Check out this example from a previous post:
getAJAX(url, function(data){
// write any code that want data from Ajax.
}, true);
This piece of code includes an IIFE function call. It is referred to as an
anonymous
function as well. This method allows for invoking inline functions and does not follow a Modular approach.
Below is the representation of a Class in javascript:
var ClassName = function(data, pubsubService) {
var items = [];
// public function
this.generateItems = function(firstItemIndex, stopIndex) {
var dataLength = data.length;
stopIndex = (stopIndex < dataLength) ? stopIndex : dataLength;
items = data.slice(firstItemIndex, stopIndex);
pubsubService.publish('itemsGenerated');
};
// private function
var getItems = function() {
return items;
};
return {
generateItems : generateItems,
getItems : getItems
};
};
In this class, generateItems
is a public function and getItems
is a private function.
Now, instead of creating a regular function as mentioned in your previous post, create a class as a module
which contains methods of that module. Create an object
and call methods like this:
Var obj = new ClassName(data,pubsubService);
obj.generateItems(firstItemIndex,stopIndex);
I believe this can aid in understanding the concept better.
Check out the following links for more information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript