8
To facilitate the work of other programmers in the company whose default is no pattern, i created a Javascript library with some form field formatting and validation functions (date, phone, CPF, CNPJ, etc.).
At the end of the process, this library allowed me a very simple call:
<input type="[data|telefone|...]" name="" value="">
At the end of the page loading i Gero a Nodelist and assign the appropriate functions to each input
in accordance with its type
.
That code then went like this: Jsbin.com
The problem with the method used is that although only one of the functions is being executed for each input
(as it should be), I’m setando ALL the functions for each object found in my Nodelist. That is, for each object I get a kilometric code, containing several functions that this object never will use.
In normal pages, where this feature is not widely used, the problem is totally inconspicuous. However, on pages where these functions are called numerous times, it is possible to notice a certain loss of performance after page loading.
And since the company’s system is to be used also on mobile devices, a small loss of performance may end up becoming a major problem.
To solve this, I thought of isolating each of the functions. Here’s my problem!
I have N functions (date, phone, Cpf, ...), within a global function (setEvents) and all I have to call these internal functions is their name in the form of string.
I’ve tried many ways, but failed to succeed.
The only way it’s worked so far is by using eval
. But as the JS Bin: "Eval is evil."
What’s the idea behind
[função a ser chamada]
? that’s passed intoa()
or where it comes from?– Sergio
@Sergio I determine which function will be called through a
this.getAttribute("type")
and call a different function for each result found.– Júlio Pradera