esdmr esdmr - 1 month ago 4
Javascript Question

How to hide what we did in javascript functions?

How to hide what we did in javascript functions?

I mean Instead Of

function alpha(){
var a=Math.PI;
//other stuff
return a;
}

alpha();//= a
alpha;//=function alpha(){...}


Be

alpha;//='presetted function placeholder'


Chrome Functions Also Following This. Example:


  • $ == function $(selector, [startNode]){ [Command Line API] }

  • return "function " + name + "(" + funcArgsSyntax + ") { [Command Line API] }";


Answer

All Chrome is doing here is setting a custom toString method in these functions. Using your example:

function alpha(){
}
a.toString = function() {
    return 'presetted function placeholder';
};

Then if you were to enter alpha in the console, in Chrome you would see:

function presetted function placeholder

Note: This doesn't really "hide" the code in any secure fashion, you could just use Function.prototype.toString.call(alpha) to get the default toString behavior.

Comments