如何防止在Javascript中更改变量值 [英] How to prevent the changing of a variable value in Javascript

查看:130
本文介绍了如何防止在Javascript中更改变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript中是否有常量?

有没有办法在javascript中声明最终值的静态,以便第三方不能更改它?

Is there a way to declare a static of final value in javascript so that it cannot be altered by a 3rd party?

我所拥有的是文章共享应用程序,免费用户受广告支持。
我想做的是阻止免费用户通过改变内容的存储变量来改变innerHTML内容。

What I have is a article sharing application, with free users being ad supported. What I wish to do is prevent the free users from altering the innerHTML content by altering the storage variable for the content.

我现在拥有的是一个计时器,每5秒在用户网站上重新加载文章的innerHTML,我将重新加载的值存储在一个变量中。

What I have at this moment is a timer that reloads the innerHTML of the article on user website every 5 seconds and I'm storing the value for the reload in a variable.

但是,如果天使使用jsbeatify探讨哪个变量掌握了删除广告的关键并改变了这一点,我们失去了收入和产品曝光率。

However, if a genius using jsbeatify explores which variable holds the key to removing the ad and alters that, we lose revenue and exposure of our products.

如何防止内部变量的改变?

How to prevent the altering of the internal variable?

这是我来的最终结果up with:

This is the end result of what I came up with:

http://jsfiddle.net/ PgdYP / 1 /

<div id="specialdiv"></div>
<input type="button" value="try to change the function i to do something different" onclick="t.i = function(){alert(data.secret);}"><BR>
<input type="button" value="set function to null out of spite" onclick="t=null;">




 <script type="text/javascript">
    e = function(){var data = { };
    Object.defineProperty(data, 'secret', {
       value: "Hello world!",
       writable : false,
       enumerable : true,
       configurable : false
       });this.z=function(){window.setTimeout("try{document.getElementById('specialdiv').innerHTML =   '"+data.secret+"';t.i.z();}catch(err) {document.body.innerHTML=err;}",5000);}}
        g = function(){};
        g.prototype = new e();e=null;window.t = {}
        Object.defineProperty(window.t, 'i', {
        value: new g(),
        writable : false,
        enumerable : false,
        configurable : false });g = null;
        window.t = Object.freeze(window.t); t = Object.seal(window.t);
        t.i.z();
    </script>

这将以打包格式呈现,只是为了使代码复制出来更难资源。
这样简单地复制和粘贴代码的工作量将最大化,并且很难实现自动化。

This will be presented in packed format, just to make it harder just to copy the code out of the source. This way the effort to simply copy and paste the code out will be maximized and will be very hard to automate.

谢谢大家的答案。

推荐答案

是的,有。至少对于对象属性,ES5为我们提供了手动更改属性描述符标志的可能性。所以我们可以这样做

Yes, there is. At least for object properties, ES5 gave us the possiblity to alter the property descriptor flags by hand. So we can do that like

var data = { };

Object.defineProperty(data, 'secret', {
    value: 42,
    writable : false,
    enumerable : true,
    configurable : false
});

这样,我们创建了一个属性 secret 数据中的值 42 ,无法修改或删除。

That way, we created a property secret with the value 42 within data, which cannot get modfied nor deleted.

这里需要注意的是, genius (就像你所说的那样)也能够发现这段代码并且只是修改它,如果他愿意,可以再次改变内容。您只是无法以这种方式创建安全的前端JavaScript。代码将始终以普通文本的形式提供给每个人。

The caveat here is, that the genius (like you called it) will also be able to spot this code and just modify that, to again be able to change the content if he wishes to. You just can't create a secured front-end javascript in that way. The code will always be available as plain-text for everybody.

为了使这更完整,您还可以创建一个对象和冻结它在某个时候,使用 Object.freeze() 和/或 Object.seal() 关闭枚举的选项在对象的属性上删除修改

To make this more complete, you can also create an object and freeze it at some point, using Object.freeze() and/or Object.seal() to shutdown the option for enumerating, deleting and modifying on properties of an object.

但再次提醒:这旨在影响您自己的代码或外部代码,以防止在运行时意外或故意修改。没有办法阻止开发人员,因为他甚至可以停止执行你的脚本,然后才能生效。

But again the word of caution: This is intended to affect your own code or foreign code, to prevent modifications by accident or on purpose at run time. There is no way to stop a developer since he can simply halt the execution of your script, before this even can take affect.

这篇关于如何防止在Javascript中更改变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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