Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what pattern to use when creating javascript class?

Tags:

People also ask

Do you use design patterns in JavaScript?

JavaScript modules are the most prevalently used design patterns for keeping particular pieces of code independent of other components. This provides loose coupling to support well-structured code. For those that are familiar with object-oriented languages, modules are JavaScript “classes”.

What are the design patterns in JS?

Some examples discussed in the article above include the constructor pattern, the module pattern, the revealing module pattern, the singleton pattern, the observer pattern, the mediator pattern, the prototype pattern, the command pattern, and the facade pattern.

How many types of design patterns are there in JavaScript?

They are categorized in three groups: Creational, Structural, and Behavioral (see below for a complete list). In this tutorial we provide JavaScript examples for each of the GoF patterns.


what is the best way to create classes ( as in OOP) in Javascript ? Right now I am using the following pattern . Is it ok ?

var myclass = (function() {
var _name;
var baseObject = {
    a: 10,
    c: function() {
        return _name + " world " + privateFunc();
        }
};
function privateFunc() { return _name + "-ba"; };

function myclass(name) {
    _name = name;
    this.x = 9;
};
myclass.prototype = baseObject;
return myclass; })();