tags:

views:

208

answers:

5
var y = new w();
var x = y.z;

y.z= function() {
      doOneThing();
      x();
   }

where w does not contain a z object but contains other objects (e.g, a, b, c)

What is x(); possibly referring too? (Again, this is JavaScript)
Is the function calling itself?

+1  A: 
var y = new w();

// Create a global variable x referring to the y.z method
var x = y.z;

y.z= function() {
        doOneThing();

      // Refers to the global x
      x();
     }

It would probably be clearer to rename x to oldZ

Greg
Is the function calling itself?
Newbie
No, it's calling what y.z was before y.z was updated
Greg
If the "var x = y.z" was *after* "y.z = function() ... " then it would be calling itself
Greg
but note, as in the spec, y.z does not exist prior to the assignment.
Kent Fredric
+1  A: 

x is a variable which, because it is scoped outside of the function that has been assigned to y.z, is accessible inside of y.z

What is going on in that code is that y is initialized to a new class of type 'w', then x is getting set to a reference to the current 'z' function, which is a member of the instance 'y' of class 'w'. Then, function 'z' is getting replaced by a new function, which calls doOneThing, and then executes the value of 'x', which as has already been established is the previous value of 'y.z', therefore, the new y.z extends the behavior of the old y.z by simple calling the old y.z before it returns.

I hope that makes sense.

Of course, given that you do say that the 'y' object doesn't have a 'z' member, than x will be undefined, and a runtime error will be thrown when you try to execute x().

foxxtrot
+1  A: 

A hard thing to wrap your mind around is that in JavaScript (and in a number of other dynamic interpreted languages too) functions are also "first class citizens". They can be manipulated just like any other variable.

Maybe it can help to visualize JavaScript functions as strings that contain the code. When called with the () syntax, JavaScript executes the code that is in the string. But otherwise they can be manipulated like any other stirng variable.

Although this analogy isn't perfect (there are actually many differences between strings and functions), it might help initially to understand this function-variable duality.

Vilx-
+2  A: 
var y = new w();
var x = y.z;  # x = undefined ( you say there is no w().z ) 

y.z= function() {
        doOneThing();
      x();  # undefined, unless doOneThing defines x 
};

however, if you manage to define x sometime before y.z(); then x() will be whatever it was defined at that time.

the following code does something analogous, as per your statements.

var y = {}; 
var x; 
y.z = function(){ 
        x(); 
}; 
y.z(); # type error, x is not a function. 
x = function(){ } ; 
y.z(); # works.  
x = undefined; 
y.z(); # type error, x is not a function.
Kent Fredric
A: 

It looks like their trying to overload the w.z() method, but it's odd because you say a z() method doesn't exist. Given that possibility, I would have written the code this way:

var y = new w();
var x = y.z;

y.z= function() {
      doOneThing();
      if (x) x();
   }

It comes down to the same thing, but will avoid an error getting thrown.

Joel Coehoorn