什么时候我应该使用变量而不是原始值
When should I use a variable vs a raw value?
何时应该定义变量?在if语句中使用原始值是否比一次性使用变量更快?例如,原始值:
if(variable == 503) {
//run code
}
VS: if(variable == anotherVariable) {
//run some code
}
我正在寻找哪一个会更快,更安全(在一个if语句和一般)。
我想说这取决于变量代表什么。如果它是一个将在多个地方重用的全局常量,那么一定要使用变量。否则,如果它是一次性使用值,则不需要变量。
我倾向于从一个值开始。然后,只要我遇到另一种情况,我重用相同的值,然后我把它移动到一个全局(或必要的作用域)变量。
编辑:在评论中进行了一些讨论之后,很明显,从长远来看,最好使用描述性变量名写出值。
经验法则是总是为变量和值使用描述性名称(并可能添加注释)。但是,由程序员自行决定是否有足够的上下文来使一个值在没有变量名的情况下存在。要考虑未来的开发人员阅读你的代码,不要假设他们会明显知道你在说什么(这是一个容易犯的错误)。
我建议总是为这些值使用描述性名称。在这个特殊的例子中,503是什么意思?
我完全同意其他的答案-只是试着给另一个灵感…我记得当我开始编码时,我过度投入于微优化,比如哪个变量会表现得更好,但毕竟我个人提出了关于变量和函数名称的明确编码风格规则:
- 其他也有编码风格。向有经验的人学习意味着一方面利用他们的经验,可以这么说,更接近"全局"风格,从而使彼此的代码具有更好的可读性。
- 选择尽可能具有描述性的名称。它不仅使你的代码更具可读性和可维护性,而且还导致关注代码结构本身内部的功能。
- 保持一致并不意味着再灵活,也不意味着在新的经历之后就不再发展自己的风格,但总的来说这是一种很好的做法。
- 让名称也告诉您类型。
- 让名称也告诉您范围。
在这些一般规则之后,这里有一些实际的例子:
我只是选择了一个非常抽象的例子来展示一般功能…
var _outerScope = 'I prefer to mark my variables with the most global scope with a leading underscore';
(function (){
var innerScope = 'while this variable has its scope inside of this function its just meant to be used in here'
if (_outerScope !== innerScope) {
'everything is a bit more clear';
}
var fSetAttrTitle = function ( $Selector, iSelector ) { // read as: "function set attribute title" awaits an "jQuery object" and "integer"
sOriginalTitle = $Selector.attr('title'); // ra: "string original title" = "jQuery object selectors attribute title"
$Selector.attr('title', 'this container has id: ' + iSelector); // ra: "jQuery object selectors attribute title" = "this container has id: " plus "integer selector"
return sOriginalTitle; // ra: "return string original title"
};
var isIdSel2inArray = false; // this should be self explanatory
var aSelector = ['#sel1', '#sel2', '#sel3']; // ra: "array selector" = [...]
var iSelector = aSelector.length; // ra: "integer selector" = length of "array selector" | normally i would use "i" instead of iSelector in this case but for illustration lets stay with it
while ( iSelector-- ) // ra: "iterate until iSelector is 0"
{
sSelector = aSelector[ iSelector ]; // ra: "string selector" is a piece out of "array selector" with number "integer selector"
if (sSelector !== '#sel2') { // ra: "if string selector is not '#sel2' then array selector is return value of set attribute title"
aSelector[ iSelector ] = fSetAttrTitle( jQuery( sSelector ), iSelector );
} else { // ra: "if string selector is '#sel2' then '#sel2' is in array"
isIdSel2inArray = true;
}
}
if (isIdSel2inArray === true) {
alert('ra: "if boolean is id sel2 in array is true alert this text"');
}
}).call(this);
if (typeof innerScope === 'undefined') {
'Of course I can not use this variable here while it has no underscore '
+ 'its not in the outer scope but the next one has an underscore so it is '
+ 'save to use it here ' + (typeof _outerScope !== 'undefined');
}
希望它有点鼓舞人心:)
相关文章:
- 在Javascript中重新分配对象变量时,原始对象会发生什么
- 阻止$scope变量副本侦听其原始副本
- 在 JavaScript 中是否有原始变量的用途
- 两个函数使用相同的变量,都不会覆盖其原始变量
- 在 JavaScript 中访问原始全局变量和属性
- 为什么原始变量的行为类似于对象
- 变量不断重置为其原始值
- 变量在操作新实例后失去原始值
- Javascript变量恢复为原始变量
- 如何在 for 循环中维护迭代变量的原始值,该循环设置事件调用,其中迭代值是函数的一部分
- 为什么Google闭包编译器在原始命名空间为空的情况下向全局命名空间添加变量
- js更改变量会影响到原始变量
- highcharts系列删除功能删除所有原始系列数据(即使存储在新变量中)
- 如何在不影响AngularJS中原始变量的情况下操作视图值
- 在JavaScript中更改局部变量会影响具有不同名称的原始全局变量
- 将Mysql查询结果传递给javascript,然后使用原始变量调用函数
- JavaScript's reverse()总是改变原始变量吗?
- 奇怪的代码——javascript (JS)中原始变量和引用变量的动态属性
- 为什么这个原始变量是通过引用而不是通过值传递给函数的?
- 通过引用javascript传递原始变量