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”.
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.
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; })();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With