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.additional();
// debug(obj1);
debug(obj1, obj1.name, obj1.private.sayHello(), obj1.sayHi());
debug(obj2, obj2.name, obj2.private.sayHello(), obj2.sayHi());
debug ("sayHello same?" , (obj1.private.sayHello == obj2.private.sayHello));
debug ("sayHi same?" , (obj1.sayHi == obj2.sayHi));
}
function Obj(thisName) {
var my = this;
my.addPrivate();
my.addDelegateCallbacks();
my.name = thisName;
my.def = "";
my.public = {
name : "public: " + my.name
};
my.protected = {};
};
/*
Obj.protected.prototype.sayHowdy = function () {
return "howdy " + this.name;
};
*/
Obj.prototype.addPrivate = function() {
var my = this;
my.private = {
sayHello : function () {
return "hello, my name is: " + my.public.name;
}
};
};
Obj.prototype.addDelegateCallbacks = function() {
var my = this;
my.delegateCallbacks = {
objLikesButt : function () {
return "yeah, who doesn't? " + my.public.name;
}
};
};
Obj.prototype.sayHi = function() {
return "hi "+this.public.name;
};
/*
Obj.prototype.protVar = {
my : this,
sayHello : function () {
return "hello, my name is: " + my.instanceVar.name;
}
};
*/
/* ************************************
_____ ___ __ __ ___ _ _ _____ ___
|_ _| __| \/ | _ \ | /_\_ _| __|
| | | _|| |\/| | _/ |__ / _ \| | | _|
|_| |___|_| |_|_| |____/_/ \_\_| |___|
************************************ */
function debug(message, value) {
var outMessage = message;
if (typeof value !== "undefined" && value !== null) {
outMessage += " [" + value + "]";
}
if ($("#log").html() != "") {
...