Javascript: attaching OOP methods to events and the 'this' keyword -
I'm new to OOP Javascript and I'm having trouble with keywords in this
.
What I am trying to achieve: I have many dom objects and not only want to pose a common event to them, but a global container (to increase runtime performance) Keep some data about
So what I do is basically this:
function class hasdodops something () {/ * This keeps node ID for processing in the INIT. Nodes = new array (); / * Process node data for faster access * / this.nodeData = new array (); This.sthAddNodes = function (/ * id * /) {/ * Add node ID for local variant (this.nodeData) * /} function init () {/ * collects data from all nodes which was added before And stores it in .nodeData * / / * Here, surprisingly, it references' window element * / addEvent (window, 'scroll', this.scroll); } Function Scroll () {/ * Does the user do stuff while scrolling the page * / / * 'This' also reference the window element here * /} addEvent (window, 'load', this, etc.); }
Later, in the document body, I could add it:
var Ctds = new classmatos (some);
Further, add the DOM elements as follows:
Ctds.addNodes (ID);
No more implementation code will be required.
Question: How to Access to JS Scroll
methods And not in the window element class instance
This is not through the keyword PS One thing I know is neither So you do not need to add and similarly: If you decide to go to class (for example, It is called A. , I know, but still I have not come anywhere.
addEvent
is an extremely basic task to engage events, it is just IE / Fx friendly and nothing is. Init
And the scroll
is a method on this example. init
and this.init
to the load code only:
addEvent (Window, 'load', init); // no. "Required
addEvent (window, 'scroll', scroll);
this.scroll
and this.init
etc), you can save the This is a reference to
and it is passed to an addEvent
:
var = an anonymous function passed to this; This.init = function ( ) {AddEvent (window, 'scroll', function () {self.scroll ()}}} this. Scroll = function () {/ * ... *}} AddEvent (window, 'load', function N () {self.init ()});
Comments
Post a Comment