This is an object literal being passed as an argument to an initializer function.
A literal is a way of defining / constructing a data structure in the program's own source code, rather than through a series of calls and / or the explicit definition of a type / class. Example:
var obj = {
budget: {
// the key is the metric id
'loadTime': {
max: 200
},
'redirectCount': {
max: 1
},
'globalJS': {
min: 2,
max: 5
}
}
};
It is equivalent to:
var obj = new Object();
obj.budget = new Object();
obj.budget.loadTime = new Object();
obj.budget.loadTime.max = 200;
...
This literal format is commonly referred to as JSON, but in reality they are different concepts (JSON is a text format, inspired by JavaScript objects, but narrower in format and more general in the application) .
// É um texto, não um literal; e repare no uso obrigatório das aspas duplas
var meuJSON = '{ "budget": { "loadTime": { "max": 200 }, "redirectCount": { "max": 1 }, "globalJS": { "min": 2, "max": 5 } } }';
var obj = JSON.parse(meuJSON); // Cria o mesmo objeto definido acima
Passing a complex object as an argument to a function is commonly used for:
-
Passing optional parameters - If your function works fine in a "standard" way but there are many details that the programmer may or may not want to adjust, then instead of an immense list of parameters it is preferable to accept an object and read from it these optional parameters. This makes it more convenient to be invoked;
-
Operate over a generic data structure - in this case, it would be the same to pass a literal or an object contained in a variable. But if you are not going to use that object outside of the context of the function, you do not have to create it and use it in two distinct steps, you can do everything in one operation (it helps to " / em> ").
What your init
method is doing, I do not know, but by the way it's a mix of one or the other (ie several of these arguments seem necessary, but not all, so receiving an object allows the programmer to choose what happens to the function without having to repeat null
, null
, null
for all parameters that do not apply).