Error in Javascript code

Asked

Viewed 363 times

4

I have a code snippet that searches a Json for the field called campo, and inserts it into an array, which cannot have repeated values. My array always returned Undefined and it took me a while to find the error.
Why do JS and even IDE consider this valid? If campo is an array, so it is obvious that after the . would call a method. In the browser there was no error.

Code snippet wrong:

while (i < listaContatos.length) {
   var dados = listaContatos[i].dados;
   for (var j = 0; j < dados.length; j++) {
       var nomeCampo = dados[j].campo;
       console.log(nomeCampo);
       if (campos.indexOf <= -1) {
           campos.push(nomeCampo);
         }
      }
   i++;
}
  • Related to your other question: http://answall.com/q/77107/132

  • @Victorstafusa did not understand why related

  • 1

    It is not of the same functionality that you are developing from contact list?

  • 2

    I’m not saying they’re duplicates or anything, I’m just saying the other question has something to do with this.

2 answers

7


It is not considered an error for some reasons.

  1. The point does not indicate that the method will be called, because for the call of a function the parentheses are mandatory. Otherwise you are just referencing the function as a variable. This makes it valid code because you are comparing a variable of type function with a variable of type number and Javascript does not restrict this due to its weakly typed nature. In this case, the comparison will always return false
  2. Second, in the case of an array the point notation can be used to refer to the array indices.

For example:

var meuArray = [];
meuArray['asd'] = 4;
console.log(meuArray.asd); // 4

It is perfectly possible to overwrite the behavior of indexOf and make that chunk of code work in an interesting way:

var meuArray = [];
meuArray['indexOf'] = -5;
console.log(meuArray.indexOf <= -1); // true
meuArray['indexOf'] = 5;
console.log(meuArray.indexOf <= -1); // false
  • 1

    Perfect in variable type Function. I didn’t know this and as answered in the other answer, I didn’t find in the documentation that it is an array attribute.

  • 2

    In a way every function is a variable. Declare like this: Function abcd() {} is equivalent to declare like this: var abcd = Function(){}

  • 2

    @fernandodelrio this is perhaps true from the point of view of execution of the function (partially, see Javascript Hoisting), but in one case, you’re creating a function called abcd and, in the other case, an anonymous function and assigning it to the variable abcd.

  • @gabrielhof is right, has some differences even

2

campos.indexOf <= -1

By performing this comparison, you are comparing a function ("fields.indexof") with an integer "-1". The comparison is valid because "indexof" is an attribute of the Array object, although it is a function, but could be an integer like the attribute "length".

  • Yes, this is correct. The question is why Javascript allows the wrong to run and does not issue any warning/error

  • I understood the real question. Answer edited.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.