S
S
Stepan2014-09-20 21:10:22
JavaScript
Stepan, 2014-09-20 21:10:22

Why is this pattern code in PHPStorm highlighted as erroneous and not working?

var module = (function () {
    var counter = 0,
        module = {};

    module.incrementCounter: function() {
        return ++counter;
    }

    module.resetCounter: function () {
        return counter = 0;
    }

    module.getCounter:function () {
        return counter;
    }

    return module;
})();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petrov, 2014-09-20
@xoma2

Because you mixed the syntax for enumerating object properties and assigning values ​​to object properties.
Either way

var module = (function () {
    var counter = 0;

    return {
    incrementCounter: function() {
      return ++counter;
    },
    resetCounter: function () {
      return counter = 0;
    },
    getCounter:function () {
      return counter;
    }
  };
})();

Either way
var module = (function () {
    var counter = 0,
        module = {};

    module.incrementCounter = function() {
        return ++counter;
    };
    module.resetCounter = function () {
        return counter = 0;
    };
    module.getCounter = function () {
        return counter;
    };

    return module;
})();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question