What is the meaning of this definition of values?

5
<script type="text/javascript">
perfBar.init({
    budget: { 
        // the key is the metric id
        'loadTime': {
            max: 200
        },
        'redirectCount': {
            max: 1
        },
        'globalJS': {
            min: 2,
            max: 5
        }
    }
});
</script>

(The above code is just an example.)

I'd like to know, what I'm doing exactly when defining these values using an external file. What would that be, an object?

    
asked by anonymous 11.06.2015 / 15:46

3 answers

8

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).

    
11.06.2015 / 16:17
2

You are using a notation called JSON, to pass the arguments to a method called init() of your perfBar .

Definition of JSON

  

JSON (pronounced ['dʒejzən]), an acronym for "JavaScript Object Notation", is a light format for computer data interchange. JSON is a subset of JavaScript object notation, but its use does not require JavaScript exclusively.

    
11.06.2015 / 15:57
0

What happens in this case is that you are calling the init () method and passing a json object as an argument.

    
11.06.2015 / 15:51