When to use Prototypes and Classes?

admin

Administrator
Staff member
Hi -

If I want to dynamically create a huge number of elements in my page for which I need to add methods and properties, is there any efficiency or memory optimization advantage to using prototypes, and if so, how would they be used?? For example, here's how I typically would dynamically create such an element:
//---------------------------------
function createDiv() {
var d = document.createElement("DIV");
d.name = "testDiv";
d.innerHTML = "foo";
d.onmouseover = over;
return d;
}
//----------------------------
function over() {
alert("Over div");
}
// DO THIS ON ONLOAD
for(var i=0; i< 1000; i++) {
document.body.appendChild( createDiv() );
}

I know that on Mozilla you can prototype the HTMLElement or its subinterfaces, etc. but on IE how would I do this, or would I really gain anything by doing so? Also, is it possible to extend a function using prototypes? It would be great to find a good reference, book or web site, that explains JS OO and classes in a more Java/C++ familiar style. Thanks for any help you can offer on this!!

- Hnorris
 
Back
Top