oop - JavaScript: Create global Object from inside Prototype dom loaded event -
I want to create an object that can be accessed worldwide after the DOM loads. My approach is to use Prototype dom: Instant loading and events.
Javascript code:
document.observe ("dom: loaded", function {var globalpao = new picArrayObject (); warning (globalPAO. PicArray [0]); // Alert [Object HTMLDivElement]}); Var picArrayObject = function () {var myPic1 = document.getElementById ('pic1'); Var myPic2 = document.getElementById ('pic2'); Var myPic3 = document.getElementById ('pic3'); Create function () {this.picArray = [myPic1, myPic2, myPic3]; } Return new build (); } MyTrigger.onClick = function ({warning (globalPAO.picArray [0]); // Alert Nothing}
Try it yourself:
Three things that I see:
-
You have to assign click handler inside the
" dom: loaded "
handler, else ID The element withtrigger
can not yet exist (in fact, this error has been shown in the error console in Safari:Writing Error: The Outcome of Expression 'MyTrigger' [invalid] is not an object. / P>
).
-
Using
new build return ()
looks overly complex. -
var globalPAO
creates a local variable that you leave, thenon
You create a global one
Better example:.
Try this yourself:
Comments
Post a Comment