2016-10-20 22 views
-3

如何隐藏我们在JavaScript函数中所做的事情?
我的意思,而不是如何隐藏我们在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

你为什么要这样做? – zzzzBov

+0

谷歌混淆,如果这是你的意思。 –

+0

@esdmr,你已经描述了*你想要什么,但不是**为什么**你想要这个功能。这似乎是[XY问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。 – zzzzBov

回答

1

所有Chrome在这里做的是设置在这些功能的自定义toString方法。使用你的例子:

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

然后,如果你在控制台输入alpha,在Chrome中,你会看到:

function presetted function placeholder 

注:这并不能真正“隐藏”的代码任何安全的时尚,你可以使用Function.prototype.toString.call(alpha)获得默认toString行为。

相关问题