In this:
somefunction = function() {
somefunction.method = function() {
//stuff
}
//other stuff
}
Is somefunction
executed everytime somefunction.method
is?
In this:
somefunction = function() {
somefunction.method = function() {
//stuff
}
//other stuff
}
Is somefunction
executed everytime somefunction.method
is?
No, somefunction is executed when you have any of these lines:
somefunction()
somefunction.call()
somefunction.apply()
new somefunction;
somefunction.method()
will only execute somefunction.method
, but that will have access to variables in the scope of somefunction
(as well as its own local variables and all global variables of course).
Not inherently (unless it is explicitly invoked in the "other stuff"). It is evaluated, but that is only CPU time and not enough to matter.
No. However you are creating a new closure each time somefunction is called. If somefunction relies on any variables outside of its scope, then the somefunction.method may change depending on when somefunction is called, thus creating a race condition (never fun to debug).
This is probably not the right way to approach the problem.
Consider
somefunction = function() {
// constructor
}
somefunction.prototype.method = function() {
//stuff
}