Instantiate a standard global function to call functions

1

I would like to know how I can create a global variable (or global element, I do not know how to call it) like in several plugins I see. Jquery example where to call any function related to it I just have to instantiate the symbol # or call the function Jquery before any internal function of the plugin.

I want in my application to be able to call something like:

MeuElemento.Cria(args);
MeuElemento.Salvar(args);
MeuElemento.Outrafunção(args);

How do I do this? Can someone please get me an example?

    
asked by anonymous 31.10.2014 / 19:24

1 answer

3

Just create a global object and add functions to it.

When you make a global object, as below, it is visible ... Well, globally.

var foo = {}; // foo só será global se essa linha rodar no escopo global, ok?

Now just add functions or whatever else you want. You can do this at any time because Javascript objects can receive new properties and methods dynamically.

foo.monstraUmPopup = function (args) {
    alert(args);
}

And you can call globally as well:

foo.mostraUmPopup("tio");

Try on your browser's console.

    
31.10.2014 / 19:35