是否可能使用闭包模拟Javascript中的常量? [英] Is it possible to simulate constants in Javascript using closures?

查看:165
本文介绍了是否可能使用闭包模拟Javascript中的常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用闭包模拟Javascript中的常量?

Is it possible to simulate constants in Javascript using closures? If so, can you please furnish me with an example?

推荐答案

Firefox和Chrome均支持 const 关键字。 IE不。所以如果你需要常量,不需要支持IE, const 不是一个坏的选择。请记住,当一个值被分配给 const 时,浏览器都不会产生运行时错误;

Firefox and Chrome both support the const keyword. IE does not. So if you need constants and don't need to support IE, const isn't a bad choice. Keep in mind though, neither browser produces a runtime error when a value is assigned to a const; the values merely remains unchanged.

否则,必须使用函数来定义不能修改的常量:

Otherwise, you must use functions to define constants that cannot be modified:

function PI() { return 3.14159; }

var area = radius*radius * PI();

当然,你可以编写不修改某些变量的代码,

Of course, you could just write code that never modifies certain variables, and maybe establish a naming scheme for such variables such that you recognize that they will never need to be modified...

// note to self: never assign values to variables utilizing all-uppercase name
var PI = 3.14159;

模拟常数的另一个选项是使用属性定义功能在某些浏览器中可用,以定义对象的只读属性。当然,因为支持属性定义的浏览器不包括IE,这不是真的帮助...(注意IE 8 支持属性定义 after a fashion ... but not on JavaScript objects)

Another option for "simulating" constants would be to use the property definition functionality available in some browsers to define read-only properties on an object. Of course, since the browsers supporting property definitions don't include IE, that doesn't really help... (note that IE8 does support property definitions after a fashion... but not on JavaScript objects)

最后,在非常设计的场景中,你可以使用函数参数作为常量(也许这是你在建议关闭时想到的)。虽然它们作为变量,但它们仍然限于它们被定义的函数,因此不能影响在它们被修改的函数之外具有相同名称的变量所拥有的值:

Finally, in very contrived scenarios you might use function arguments as constants (perhaps this is what you were thinking of when you suggested closures?). While they behave as variables, they remain scoped to the function in which they are defined and cannot therefore affect the values held by variables with the same name outside of the function in which they are modified:

var blah = 3;
var naw = "nope";
(function(blah, naw)
{
  blah = 2;
  naw = "yeah";
  alert(naw + blah); // "yeah2"
})(blah, naw);

alert(naw + blah); // "nope3"

请注意,类似的东西是通常由jQuery插件使用,但是相反的原因:jQuery代码通常使用 $ 简写来引用jQuery对象,但是库旨在继续工作,即使一些其他代码重新定义该符号;通过使用 $ 参数将库和插件代码封装在匿名函数中,然后传递 jQuery 作为参数,代码与其他库可能对 $ 的价值隔离。

Note that something similar to this is commonly used by jQuery plugins, but for the opposite reason: jQuery code is usually written using the $ shorthand to refer to the jQuery object, but the library is intended to continue working even if some other code redefines that symbol; by wrapping library and plugin code in anonymous functions with a $ parameter and then passing in jQuery as an argument, the code is isolated from changes other libraries might make to the value of $ later on.

这篇关于是否可能使用闭包模拟Javascript中的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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