Problem with eval() in javascript
could somebody help me out in understanding this javascript piece of code :
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)));}
Sorry for irritating you guys with least details. Actually I got the code from - forum.fusioncharts.com/topic/8012-fusion-charts-on-android
It is abput using Fusioncharts in android using Phonegap. So Fusio开发者_JAVA百科ncharts.js contains this code nad I am not an expert in javascript and did not get it. So asked for the help. But by looking at different answer I feel full src code is not available here.
thanks
snehaA function is defined that takes 6 parameters:
function(p,a,c,k,e,r)
It sets the parameter e to yet another function, that takes the initial "c" parameter as a parameter:
e = function(c)
The contents of that function then check if "c" is less than "a". If it is, it returns an empty string. Otherwise, it runs the same function again (e) with the integer value of the parameter c divided by the parameter a.
return(c<a?'':e(parseInt(c/a)));
Parameters p, k and r go unused.
Since the only value that can be returned is an empty string, you shouldn't expect much happening.
As to what the actual use is - Beats me.
This looks like a generated (packed code) - a strategy that is generally used to reduce the size of the original Javascript code and/or make it harder for someone to work out what is going on.
There must be some logical scripting going on here, which is obfuscated as a result of the packing. If you have the script at hand, you can use this to try unpacking it, to figure out what's going on behind it.
Written more legibly, your code is as follows:
function(p,a,c,k,e,r){
e = function(c) {
return c < a ? '' : e(parseInt(c/a));
};
}();
So you're defining a function that takes 6 parameters, defines a function e()
(in its local scope) that takes a single parameter and recursively calls itself so long as its parameter is greater than the second parameter to the original function (a
), eventually either returning ''
or recursing infinitely for any value of a
that is between 0 and 1 (assuming a positive value of c
that is initially greater than a
).
The outermost function will be invoked by the eval()
statement, but the inner one (e()
) will not. Since e()
is scoped locally to the outermost function, that makes running this code kind of pointless, at least in isolation as it is shown here. It doesn't seem like it really does anything very useful. Especially since the eval()
is not providing any value for a
, so when the code executes a
will be undefined, meaning that e()
would not do anything useful, even if it were called and even if its intended behavior could be accurately described as "useful".
Also, expect people to scold you for using eval()
.
精彩评论