Object-Oriented Javascript: Order From Chaos
Object-Oriented Javascript: Order From Chaos
@kvangork
#OOJS
Object-Oriented Javascript
order from chaos (we hope)
function bfIsAlphaNumeric( cfield )
{
cfield.value = TRIM2(cfield.value);
for ( i = 0 ; i < cfield.value.length ; i++)
{
var n = cfield.value.substr(i,1);
if ( n != 'a' && n != 'b' && n != 'c' && n != 'd'
&& n != 'e' && n != 'f' && n != 'g' && n != 'h'
//...
&& n != '8' && n != '9'
&& n != '_' && n != '@' && n != '-‐' && n != '.' )
{
window.alert("Only Alphanumeric are allowed.\nPlease re-‐enter the value.");
cfield.value = '';
cfield.focus();
}
cfield.value = cfield.value.toUpperCase();
}
return;
}
everything is an
Object
even functions
http://www.flickr.com/photos/sanchtv/4192677571
every
Object
has a
Prototype
http://www.flickr.com/photos/macwalsh/4403701509
Tools
Container.prototype.addItem = function(item) {
this.items.push(item);
}
Container.prototype.getCount = function() {
return this.items.length;
}
var Container = function(){
this.items = []; //empty array to hold items
};
Container.prototype.addItem = function(item) {
this.items.push(item);
}
Container.prototype.getCount = function() {
return this.items.length;
}
addItem: function(item) {
if(this.getCount() < this.maxItems) {
LimitedContainer.superclass.addItem.call(this, item);
}
}
});
var Container = function(){
this.items = []; //empty array to hold items
};
Container.prototype.addItem = function(item) {
this.items.push(item);
}
Container.prototype.getCount = function() {
return this.items.length;
}
addItem: function(item) {
if(this.getCount() < this.maxItems) {
LimitedContainer.superclass.addItem.call(this, item);
}
}
});
http://www.flickr.com/photos/thomasroche/2481517741
Bearded Men of the 21st Century (1939)
Config Objects
new LimitedContainer(3);
Config Objects
function LimitedContainer(config) {
mixin(this, config); //who knows what you'll need
this.maxItems = config.maxItems || 3;
}
Model View
var DS = {};