如何知道如果一个函数是从JavaScript控制台叫什么? [英] How to know if a function is called from the javascript console?

查看:158
本文介绍了如何知道如果一个函数是从JavaScript控制台叫什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道怎么才能隐藏的JavaScript控制台功能,使其不能从它调用。

I want to know how can we hide a function from the javascript console so that it can't be called from it.

为了让你更好地理解这个问题,假设我有一个JavaScript函数,使用Ajax在数据库上增加了一些记录,而其他人可以使用此功能从控制台,因为他希望在数据库中添加任意多条记录。我希望函数从Web应用程序,而不是从控制台叫......我试图混淆code把它藏起来,但我们仍然可以找到从谷歌Chrome浏览器的JavaScript控制台。

To let you better understand the problem , suppose that I have a javascript function that adds some records on the database using Ajax, and anyone else can use this function from the console to add as many records as he wants on the database. I want the function to be called from the web application and not from the console... I tried to obfuscate the code to hide it but we can still find the function from Google Chrome javascript console.

推荐答案

无论你多么模糊和隐藏你的code,可以可以在控制台中执行客户端上执行任何JavaScript你应该总是在服务器端运行的权限/安全性检查,如果你想拥有真正控制这些功能的安全性。

话虽这么说,你可以调整你的code内的立即调用函数EX pression,不会从控制台像往常一样,像这样被调用一样容易:

That being said, you can restructure your code inside an immediately invoked function expression which will not be callable as easily from the console as usual like so:

(function() {
    var myUncallableFunction = function() { alert('meow'); }
})();

功能 myUncallableFunction 将只能从父函数中调用,因为它是一个局部变量的父功能是它因此而不可访问之外。

the function myUncallableFunction will only be able to be called from within the parent function as it is a local variable to that parent function and is therefore inaccessible outside of it.

您仍然可以调用这个函数的父但是里面像这样:

You will still be able to call this function inside of that parent however like so:

(function() {
    var myUncallableFunction = function() { alert('meow'); }
    myUncallableFunction();
})();

这篇关于如何知道如果一个函数是从JavaScript控制台叫什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆