Is there a difference between "variavelX: Function(){...}" and "variavelX = Function(){...}"?

Asked

Viewed 56 times

3

Exemplo.prototype = {
    minhaFuncao: function() {
        this.variavelX++;
    }
}

I found this sign : apparently acting as an assignment signal (=), but I’m not sure if he acts as an assignment sign or if he acts in a different way underneath the scenes.

Is there any difference between minhaFuncao: function(){...} and var minhaFuncao = function(){...}?

  • 1

    Hi Lucas, welcome to Stack Overflow. Your question was not very clear to me, could you please edit the question and add more details as well as a possible code example?

  • I edited the question

  • 1

    Perfect. I gave a complemented to try to help, feel free to correct my editing.

1 answer

4


var minhaFuncao = function(){...}

You can use it anywhere in your code, and regardless of where you declare it, minhaFuncao will be of global scope.

Already

minhaFuncao: function(){...}

as you can see, is in the prototype of Exemplo, then basically you use this form for defining and assigning properties within objects, keeping the scope of it, and to access it you will use an instance of Exemplo.

var ex = new Exemplo();
ex.minhaFuncao();

Browser other questions tagged

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