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 and Scroll methods And not in the window element class instance .

This is not through the keyword , I know, but still I have not come anywhere.

PS

  • addEvent is an extremely basic task to engage events, it is just IE / Fx friendly and nothing is.
  • Code I writing is already functional, but in procedural form, I wanted to do it OOP.
  • As a short sub-question, I get some kind of effect, the guest / setter methods in JavaScript are discouraged. Is it okay if I use them?
  • One thing I know is neither Init And the scroll is a method on this example.

    So you do not need to add init and this.init to the load code only:

      addEvent (Window, 'load', init); // no. "Required  

    and similarly:

      addEvent (window, 'scroll', scroll);  

    If you decide to go to class (for example, 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 ()});  

    It is called A.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

kernel - How to create a "Block device" in Windows -