What Javascript programming patterns do you use?

I am reviewing a lot of my code, and I keep returning to the question: "What structure should I base on all my JS classes?"

Some sample templates: http://www.klauskomenda.com/code/javascript-programming-patterns/

As a result, I had a mixed approach, with the basic pub / sub functions. I do not use a prototype or module template, I define public / private properties / methods inside the constructor.

For instance:

function ClassName(){
    var _privateVar = 'private';
    this.publicVar = 'public';
    function _privateMethod(){};
    this.publicMethod = function(){};
}

What patterns do you often use in JS? For what? Regular sites? Full web application? What made you choose one pattern over another?

Or do you think this is less important than I think?

Thanks in advance.

EDIT: , , . - ? ?

+3
2

, . .

/, , .

. , .

+2

tinyHippos.

,

:

var nameSpace = {};

// class constructor
nameSpace.SomeClass = function (arg1, arg1) {

    var _privateProperty = "foo";

    function _privateMethod() {}

    // return public methods
    return {
        publicProperty: "bar",
        publicMethod: function  publicMethod() { }
    };

};

var obj = new nameSpace.SomeClass("arg1", "arg2");
+1

Source: https://habr.com/ru/post/1780315/


All Articles