How to “properly” create a custom object in JavaScript?

后端 未结 15 1984
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 08:07

I wonder about what the best way is to create an JavaScript object that has properties and methods.

I have seen examples where the person used var self = this<

15条回答
  •  情话喂你
    2020-11-21 08:25

    You can also do it this way, using structures :

    function createCounter () {
        var count = 0;
    
        return {
            increaseBy: function(nb) {
                count += nb;
            },
            reset: function {
                count = 0;
            }
        }
    }
    

    Then :

    var counter1 = createCounter();
    counter1.increaseBy(4);
    

提交回复
热议问题