如何隐藏我们在javascript函数中所做的事情?

时间:2016-10-20 16:51:56

标签: javascript function

如何隐藏我们在javascript函数中所做的事情?
我的意思是代替

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

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

alpha;//='presetted function placeholder'

Chrome Functions Also Following This。例如:

  • $ == function $(selector, [startNode]){ [Command Line API] }
  • return "function " + name + "(" + funcArgsSyntax + ") { [Command Line API] }";

1 个答案:

答案 0 :(得分:1)

所有Chrome在此处都在为这些功能设置自定义toString方法。使用您的示例:

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

然后,如果您要在控制台中输入alpha,则可以在Chrome中看到:

function presetted function placeholder

注意:这并不是以任何安全的方式“隐藏”代码,您只需使用Function.prototype.toString.call(alpha)来获取默认的toString行为。