Object structure tests

testing out scoping and prototyping

by bladnman

HTML

<script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
<input type=button id="theButton" value="run test" class="runButton">

<div id="log" class="log"></div>

CSS

.runButton {
    width:   125px;
    margin:  20px;
}
.log {
   padding:10px; 
    margin: 20px; 
    border: 1px dotted #ccc; 
    color:#888; 
    font-face: arial; 
    font-size:12px; 
    background: #fbfbfb; 
}

JavaScript

/* ************************************ */

function runTest() {
    
    var obj1 = new Obj("obj1");
    var obj2 = new Obj("obj2");
    
    debug(obj1, obj1.internal.sayHi() );
    debug(obj1, obj1.ext.sayHi() );    
    debug(obj1, obj1.external.sayHi() );

}



function Obj(thisName) {
    var my  = this;
    
    my.name = thisName;
    my.def  = "";
    
    my.internal = {
        name : "internal: " + my.name ,
        
        sayHi : function() {
            return "hi, internal from: " + my.name;
        }
    };
    
};

Obj.prototype = {
    
     external : {
         
        sayHi : function() {
            return "hi, external from: " + my.name;
        }
        
    }
    
};

Obj.prototype.ext = function () { };
Obj.ext.prototype.sayHi = function () {
    return "hi, ext from: " + this.name;
};
 


/* ************************************ 
 _____ ___ __  __ ___ _      _ _____ ___ 
|_   _| __|  \/  | _ \ |    /_\_   _| __|
  | | | _|| |\/| |  _/ |__ / _ \| | | _| 
  |_| |___|_|  |_|_| |____/_/ \_\_| |___|
                                         
************************************  */
function debug(message, value) {
    var outMessage = message;

    if (typeof value !== "undefined" && value !== null) {
        outMessage += "&nbsp;&nbsp;&nbsp;&nbsp;[" + value + "]";
    }
    if ($("#log").html() != "") {
        $("#log").append("<br>");
    }
    $("#log").append(outMessage);

    if (window.console && console.log) {
        console.log(arguments);
    }
}


$("#theButton").live('click', function() {
    runTest();
});


// ADD A SIMPLE "log" handler
// Avoid `console` errors in browsers that lack a console.
function safeifyConsoleAndAddLog() {
    var method;
    var noop = function noop() {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
     ...