Have some problem playing the function for variable

Asked

Viewed 267 times

8

Can anyone explain to me if you have a problem playing a function for the variable, for example, in this case I’m talking about the var marcar. In this code is working, I don’t know if it causes a performance loss or something. The marcar will only run at the moments that 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();
    }

});
  • 1

    There is no problem, Marcelo.

3 answers

9

In Javascript you can reference a variable to a function without problems. However the behavior will be different depending on the cases.

Appointed function:

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

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

Anonymity function:

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

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

Creating a 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 using context:

But if the method has context dependency you may have problems doing this kind 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 object ẁindow that in the case does not have the property texto and so he 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');

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

2

Marcelo, this is one of the ways to declare methods in javascript. Example:

var myvar = function() {
  return true;
}

Besides that way, there’s this:

function myfunc() {
  return true;
}

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

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

var myobject = {
  frutas = ['Maçã', 'Morango', 'Uva'],
  carros = ['Gol', 'Uno']
};

2

Browser other questions tagged

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