Do you have any problems while playing the function for variable

8

Can anyone explain to me if I have a problem while playing a function for the variable, for example, in this case I'm talking about var marcar . This code is working, I do not know if it causes a performance loss or something else. Will marcar only run at the times when it will actually run correctly?

    $('input:checkbox#marcar').click(function() {
    var valor_id = $(this).data('valor-id'),
        checked  = $(this).is(':checked');

    var marcar = function() {
        $.ajax({
            url: 'marcar.php',
            data: {'valor_id' : valor_id},
            success: function()
            {
                alert('Sucesso!!!');
            }
        });
    }

    if(checked === false)
    {
        $.ajax({
            url: 'verifica.php',
            data: {'valor_id' : valor_id},
            success: function(retorno) {
                var resp = $.parseJSON(retorno);
                if(resp.success === true)
                {
                    var cont = confirm(resp.msg);
                    if(cont === true)
                    {
                        marcar();
                    }
                    else
                    {
                        $this.prop("checked", true);
                    }
                }
                else
                {
                    marcar();
                }
            }
        });
    }
    else
    {
        marcar();
    }

});
    
asked by anonymous 11.03.2014 / 21:39

3 answers

9

In Javascript you can reference a variable to a function with no problems. But the behavior will be different depending on the cases.

Named Role:

When you define a function named function nome() {} it is placed throughout the execution scope, for example:

foo(); // alert('ola')
function foo() { alert('ola'); }

Anonymous function:

If you define in a variable it will only exist from the moment the variable is assigned the function (the variable exists from the beginning of the context), example:

foo(); // undefined
var foo = function () { alert('ola'); }
foo(); // alert('ola')

Creating Function Reference:

It is still possible to import methods from other objects, but be careful!

var obj = {
    foo: function () { alert('ola'); }
};
var foo = obj.foo;
foo(); // alert('ola');
obj.foo(); // alert('ola');

Function reference that uses context:

But if the method has context dependency you may have problems doing this type of mixin, for example:

var obj = {
    texto: 'ola',
    foo: function () { alert(this.texto); }
};
var foo = obj.foo;
foo(); // alert(undefined);
obj.foo(); // alert('ola');

In this case when you played obj.foo in foo the closest context to it is the ẁindow object which in the case does not have the texto property and therefore it returns undefined .

var obj = {
    texto: 'ola',
    foo: function () { alert(this.texto); }
};
var obj2 = {
    texto: 'tchau'
};
obj2.foo = obj.foo;
obj2.foo(); // alert('tchau');
obj.foo(); // alert('ola');

Note that in this example obj2 has texto property other than obj and the function will return the object context where it is.

    
11.03.2014 / 22:17
2

Marcelo, this is one way to declare methods in javascript. Example:

var myvar = function() {
  return true;
}

In addition to this, you have this:

function myfunc() {
  return true;
}

As well as this way, you can define objects and arrays in varying degrees. Example:

var myvar = ['Maçã', 'Morango', 'Uva'];

var myobject = {
  frutas = ['Maçã', 'Morango', 'Uva'],
  carros = ['Gol', 'Uno']
};
    
11.03.2014 / 22:10
2

There is no problem as this is a very common practice.

In the article Javascript: variable as function and object you can see in detail how it works.

    
11.03.2014 / 22:19