Is there a difference between “variableX: function () {…}” and “variableX = function () {…}”?

Posted on

Question :

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

I’ve found this : signal apparently acting as an assignment sign ( = ), but I’m not sure if it acts as a token of assignment or if it acts differently beneath the wipes.

Are there any differences between minhaFuncao: function(){...} and var minhaFuncao = function(){...} ?

    

Answer :

var minhaFuncao = function(){...}

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

Now

minhaFuncao: function(){...}

As you can see, it’s in the prototype of Exemplo , so basically you use this form to define and assign 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();

    

Leave a Reply

Your email address will not be published. Required fields are marked *