Revealing Prototype Pattern error prone point

Revealing Prototype Pattern error prone point

by Ilia Tretiakov

HTML

<h2>Revealing Prototype Pattern error prone point</h2>
<button id="gogo">Go Go</button>
<div id="log"></div>

JavaScript

var Ctor = function(){
    this.ctorState = 'OK'
};

Ctor.prototype = function(){
    var self = this;
    
    var dummy = 4;
    
    this.point2 = 'prototype';
    var externalFunc = function(){
        printLogUnsafe('externalFunc-', this);
        
        innerFunc();
        innerFuncCall.call(this);
    };
    var innerFunc = function(){
        printLogUnsafe('innerFunc-', this);
    };
    var innerFuncCall = function(){
        printLogUnsafe('innerFuncCall-', this);
    };
    var printLogUnsafe = function(text, thisObj){
        thisObj = thisObj || {};
        $('#log').append('<p>' + text + '; ctorState=' + thisObj.ctorState + '</p>');
    }
    
    return {
      externalFunc: externalFunc,
      printLogUnsafe: printLogUnsafe
    };
}();

var obj = new Ctor();

$('#gogo').on('click', function(){
    $('#log').html('');
    
    obj.externalFunc();
});