26 lokakuuta, 2014

Part 5 'strict mode' JavaScript

Where is callee and caller in strict mode?

In the good old days (yesterday) there was a time when you could call your JavaScript function and that function was so familiar that he/she could know who was the caller, like this:

var foo = function ( ) {
  alert("hi, I am function: " + arguments.callee.name);
  alert(arguments.callee.caller.name + " called, can I help?");
}; // end
foo();


But now we live in ECMA 5.1  'strict mode' and that is just not happening anymore. Really.
If you do use it you will get this kind of TypeErrors:

TypeError: undefined is not an object (evaluating 'this.caller')
TypeError: undefined is not an object (evaluating 'this.callee')

What to do? How can we... yes, relax, there is something we can do, but not anything.
Do not rush into strict mode if you are in the middle of something, but start your next project from scratch with strict mode on!

Not a solution but something
Ok. Let us see. Imagine you have a small app -object with bunch of privileged methods:

'strict mode'

var app = (function () { // app & private area start
var secret,
privateSetter = function (x){
// alert("private: " + x);
secret = x;
return true;
}; // private area end
return { // privileged area start
'get': function (){ // getter
return secret;
},
'set': function (c){ // setter
return privateSetter(c);
}
    }; // privileged area end
} () ) ; // app end

You can get those method variable names like this:

var getMethodNames = function (obj){ // ret: Array of method names
var names = [];
for(var key in obj){
if(obj.hasOwnProperty(key)){
names.push(key);
} // if
} // for end
return names;
} // alert( getMethodNames(app));  // 'get,set'

Method privateSetter is not seen here because it is encapsulated in private area of object
You can get whole source code of any method with:

alert(app['set']);

But it's useless because function is anonymous. But what if we name the functions, they don't have to be anonymous, actually they can have any name, also different than method -variable names. So lets alter the code like this, look bold text after function -words:

var app = (function () { // app & private area start
var secret,
privateSetter = function (x) {
// alert("private: " + x);
secret = x;
return true;
}; // private area end
return { // privileged area start
'get': function get () { // getter
return secret;
},
'set': function SET (c){ // setter
return privateSetter (c);
}
    }; // privileged area end
} () ) ; // app end

Not all functions are anonymous anymore. So, try running following code, getFuncName 
will return the altered (new) name of method.  (SET. With kapital, for clarity here)


var getFuncName = function (obj, key) { // return the name of method
if( obj.hasOwnProperty(key) ) {
return obj[key].name ? obj[key].name : "anonymous";
} else {
return "No_Method_" + key;
}
}
alert(getFuncName(app, 'set')); // SET

If function have always same name as corresponding method variable, this might be useful habit, so then you can ask a function it's name sometimes? But why? If you find yourself in a situation like that ask yourself a question: Why? Why does the poor function have to have a name, and it should even know it? Most functions can be anonymous and happy. So I am not the guy who told you that you should name all your functions. Keep private functions anonymous, please, for securitys sake. But this might be something interesting about 'strict mode' for somebody...
This is something what I am working on, so I might put more about strict mode JavaScript here if anybody is interested... maybe not. How many? Just you, ok. Let me know. See you.