1
//1  
var object1 = function(o){

   // some code

};  

//2  
var object2 = new object1({

   method1: //some code  
   method2: //some code  
   method3: //some code

});

//3


document.onkeydown=function(e){     
  var eventKey = e.keyCode || e.charCode;   
  var sel = Coverflow.selected;  
  if(eventKey == 39 || eventKey == 37) {  
        if(eventKey == 39) sel++;  
      else if(eventKey == 37) sel--;  
      sel = sel.limit(0, Coverflow.getListLength() - 1);  
      Coverflow.select(sel);
    }  
}

well, my question are:

  1. what happens when object1 is created, what does the = function() part mean?
  2. is object2 pointing to object1. is that assignment creating a inheritance?
  3. if possible can someone explain what going on in document.onkeydown(). the code is for a coverflow effect. also this function is not been called anywhere else in the program then how does it get executed?

thank you

Kamil Klimek
  • 12,274
  • 2
  • 37
  • 55
user2132383
  • 219
  • 2
  • 4
  • 18

3 Answers3

2

What happens when object1 is created, what does the = function() part mean?

Functions, in JavaScript, are first class objects. They can be passed around and assigned like any other function.

Is object2 pointing to object1? Is that assignment creating an inheritance?

No. object2 is an instance of object1. This is like treating object1 as a class definition.

how does document.onkeydown get executed?

When a key is pressed down, an event will be fired. Code in the document object (which is provided by the browser) listens for that event and calls that function (if it exists).

MDN has some documentation on how events work if you want more details.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
0

1 - its a function named object1, defined at runtime, more here

2 - new instance of function with a parameter passed to it (referenced as 'o' in function declaration)

3 - function is bound to onkeydown event so it gets executed every time key is pressed on document, more info here

Community
  • 1
  • 1
Gatekeeper
  • 1,516
  • 2
  • 21
  • 33
0

JavaScript does not have a notion of class, but it is possible to have the similar behavior. So I will call objects/functions that may be instantiated classes.

  1. object1 is some kind of class defined by constructor, function(){...} part is a constructor of that class.
  2. object2 is a reference to instance of object1 class, which was instantiated by calling constructor of object1 class with 1 parameter, which itself is an object.
  3. onkeydown is a reserved property of DOM objects for handlers of keydown event. Once this event is fired by browser it is handled by relevant function (if it is exists)
Vadim
  • 8,355
  • 4
  • 39
  • 47