Search code examples
javascriptdesign-patternsparadigms

Comparison of Javascript Pattern/Paradigms / Best practice of design


I am compiling a list of biases(both proponents and antiponents) to explain to my team lead why our current javascript design is outdated. This is an open-ended question, I am looking for 3rd party analysis on the pro's and con's of the following to paradigms.

Our existing paradigm on the system looks like the following

var class= (function () {

    var property1 = 8;

    var method2 = function (value) {
        var instance = this;
        return value;
    };

    var method1 = function () {
        var instance = this;
        return "Hello";

    };

    return {
        init: function () {
            alert(methodTwo(methodName()));
            method2();
            console.log(state);
        },
        method1: function(){
            method1();
        }
    }

})();

I want to convince him of the following as a best practice

namespace.MyClass = function(){
 var instance = this;
 instance.Property1 = 8;
 instance.Method1 = function(){};
 instance.Method2 = function(){};
}
var myobject = new namespace.MyClass();

Solution

  • Immediately Invoked Function Expressions:

    • +Scope Protection
    • +Works on older browsers

    Ecmascript 6:

    • +Modern standard
    • -No compatibility for ie 8 and lower

    PseudoObjectOrientated:

    • +More OO orientated
    • +Delayed invocation
    • -Overuses reliance on instance of "this pointer"