OO Javascript help
Posted: Fri Jun 13, 2008 3:35 pm
Hey, Javascript newbie here. I have been trying to figure out how JQuery, Prototype and other frameworks go about making JS more OO like. I understand the $ function, but can't figure out how to add methods to it. I'm starting off with a simple example.
Code so far...
Code so far...
Code: Select all
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
Element.extend {
hide: function() {
this.style.display = 'none';
},
show: function() {
this.style.display = '';
}
}
window.onload = function() {
$('myTestElement').hide(); //isn't working
}