0

I don´t understand the key word 'in'.

it´s a conditional builder?

This is the context:

if ($(element).attr('id') in special_validations)
            {
                var validation = special_validations[$(element).attr('id')]($(element).val());
                if (validation == true && typeof validation == 'boolean')
                    $(element).parent().removeClass('has-error').addClass('has-success');
                else
                {
                    total_errors++;
                    $(element).parent().addClass('has-error').removeClass('has-success');
                    if (typeof validation == 'string')
                        $('<label class="control-label" for="'+$(element).attr('id')+'">'+validation+'</label>').insertAfter(element);
                    else
                        $('<label class="control-label" for="'+$(element).attr('id')+'">Formato no válido</label>').insertAfter(element);
                }
            }
Funny Frontend
  • 3,807
  • 11
  • 37
  • 56

2 Answers2

1

The in keyword checks if a certain key is in an object. Note that this checks the key, not the value.

So, your example checks if the element's ID is a key in the special_validations object.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
-1

in a Conditional test

in represents something inside of something else. So we can use it as a conditional test to check if something is in something else.

In an Object, we could check if a property name exists inside it:

var my_object = {first:'a',second:'b',third:'c',fourth:'d'}

console.log( 'first' in my_object ) // returns true
console.log( 'me' in my_object ) // returns false

In an Array, we could check if a key exists inside it:

This array has five keys in it.

var my_array = [ 'a' , 'b' , 'c' , 'd' , 'e' ]

Each element can then be referenced later with a numeric key like so:

my_array[0] = 'a'
my_array[1] = 'b'
my_array[2] = 'c'
my_array[3] = 'd'
my_array[4] = 'e'

console.log( 0 in my_array ) // returns true
console.log( 1 in my_array ) // returns true
console.log( 2 in my_array ) // returns true
console.log( 3 in my_array ) // returns true
console.log( 4 in my_array ) // returns true
console.log( 5 in my_array ) // returns false

So the in operator refers to the conditionality of an Object containing a certain property name.


in looping Arrays with numeric keys

Lets say you have an array like so:

var my_array = [ 'a' , 'b' , 'c' , 'd' , 'e' ]

You can use a for loop to iterate through each of these:

for(var key=0; key<my_array.length; key++){
    var this_element = my_array[key]
}

But you can also do this with the in operator:

for(var key in my_array){
    var this_element = my_array[key]
}

So the in operator refers to each key inside of an Array.


in looping Objects with NON-numeric keys

The in operator is commonly used with a for loop through Object property names in JavaScript.

This is especially useful for Objects that have properties, which are somewhat treated like Array values.

A lot of times you many not be sure how many properties exist inside of your object or even what the property names are.

For example, this is a typical Object:

var my_object = {first:'a',second:'b',third:'c',fourth:'d'}

Each property can then be referred to with their property name, like so:

my_object['first']  = 'a'
my_object['second'] = 'b'
my_object['third']  = 'c'
my_object['fourth'] = 'd'

So here, we see that the properties are not defined with simple integer numbers like an Array. Therefore we must use the in operator to walk us through the Object properties.

Object properties rarely have numeric keys like JavaScript Arrays do. So there is no way to conduct a traditional numeric for loop.

So instead of defining a for loop traditionally with definite numeric bounds, you use in to write the following without including any definite bounds into the for loop parameters, like so:

for(var element in my_array){}

So the in operator refers to each property name of an Object.


Dangers of using in with for

Objects have inherent properties.

For example, an Array is a type of Object and all Arrays have the length property in their object prototype.

Just by defining an empty object, there are certain properties that come with it. A for loop using in will iterate over these undesired properties and 'could' break your code.

So, even in an empty Array:

console.log( 'length' in new Array() ) // returns true

This same principle may cause a for loop to iterate over undesired properties, depending on your web-browser and version.

FactoryAidan
  • 2,484
  • 1
  • 13
  • 13
  • *"So the JavaScript operator `in` refers to each element inside of an array."* Uhm what? Can you elaborate on that? *"[...] which are somewhat treated like array elements [...]"*: this is just confusing. *"That is an object like an array"* What's an example of an object that is *not* like an array? Objects are simply key-value stores. Arrays are a specialized version of key-value stores where keys are numeric. Saying that an object is "like an array" is confusing at best and simply wrong at worst. – Felix Kling Mar 04 '15 at 08:05
  • *"So the in operator refers to each element inside of an array."* Not exactly true. `for/in` iterates over the object's **property names**. If you have an array it means it iterates over the **indexes** of the array. You can then use that index to refer to the corresponding element. But again, you are describing the `for/in` loop, which has nothing to do with the `in` operator (which is what the question is about). – Felix Kling Mar 04 '15 at 08:22