What is x (); To refer to?
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 yz (); then x () will be whatever was defined at that time.
the following code does something similar, according to 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.
source to share
x is a variable that, since it is outside the function that was assigned to yz, is available inside yz
What happens in this code is that y is initialized with a new class of type w, then x gets a reference to the current function z, which is a member of the instance y 'of class' w'. The 'z' function is then replaced with a new function that calls doOneThing and then executes the 'x' value, which is already set to the previous 'y.z' value, so the new yz extends the old yz behavior by simply calling the old yz before returning it ...
Hope this makes sense.
Of course, given that you say that object 'y' has no member 'z' than x would be undefined and when trying to execute x ().
source to share
The tough thing to trick your mind is that in JavaScript (and many other dynamically interpreted languages), functions are also "first class citizens". They can be manipulated just like any other variable.
Maybe it can help render JavaScript functions as strings containing code. When called with the syntax (), JavaScript executes the code that is on the line. But otherwise, they can be manipulated like any other variable variable.
While this analogy is not perfect (there are actually many differences between strings and functions), it can help you understand this duality of a function variable first.
source to share
It looks like they are trying to overload the wz () method, but this is weird because you are saying that the z () method does not exist. Given this possibility, I would write the code this way:
var y = new w();
var x = y.z;
y.z= function() {
doOneThing();
if (x) x();
}
It boils down to the same thing, but avoids the error.
source to share