Ok guys for anyone watching, I have found my answer. But first, I will clarify a little on the code...
q=new function(){
The reason for using new function
is that I had never really used/practiced with self-executing - anonymous functions before and this was the only way I knew how!
q(x) === function Q(x){...}
q
is purposefully global!
u
is shorthand for undefined
, var u === var u=undefined
..
It's true I can write this !== undefined
, but q_ex()
will be used, in future, for multiple object types, from strings to numbers etc...
This level of 'same-name variables' is quite acceptable for it's purpose!
q=function Q(slf){
The reason behind naming the function is purely astetic, and is generally removed once coding is tested.
ex=q_ex.call(slf);
// ex is not declared with var so this will create a global variable, bad bad bad
Well spotted! Though this is a some-what simplified version of the code that is causing the problem, as such ex
doesn't need declaring here for the problem to arise.
One last thing... I understand there seems to be no reason for the outer casing of the 'Q' function, but if you look closer... My only global variable is 'q'! If not for the outer casing: u
, q
, q_ex
and any future object would be global and this is NOT the requisite.
Ok then, for my answer...
'use strict';
Function.prototype.call
Strict mode
The solution to my woes...
var q=(function(){
var u,q;
q=function Q(slf){
console.log('slf ⇒',slf);
var ex=q_ex.call(slf);
console.log('ex ⇒',ex);
};
return q;
function q_ex(){
'use strict';
console.log('this ⇒',this);
return (this!==u && this!==null);
};
})();
q();
slf ⇒ undefined
this ⇒ undefined
ex ⇒ false
Thanks for all your help guys, I can now move on with the bigger picture!
I will try to pull myself away from using new function
and replace it with (function(){})()
.
Hope this helps.