编程语言惯用法“if object.value == some_value then object.value = some_other_value”
我多次想知道自己是否有,如果没有,为什么不存在以下伪代码的惯用语/快捷方式:
if object.value == some_value then object.value = some_other_value
例如,在 JavaScript 中我有时会写:
if (document.getElementById("toggledDiv").style.display == "block") {
document.getElementById("toggledDiv").style.display = "none";
}
这似乎相当乏味。这个习惯用法有名字吗?在常见的编程语言中是否有更简洁的语法?
谢谢你!
编辑:更准确地说,我不关心大括号,但您必须至少多次引用该属性。我想要类似的东西(伪代码):
test ( object.value ):
if (it > 0) it = 0;
else it -= 1;
例如:
test (document.getElementById("toggledDiv").style.display):
if (it == "block") it = "none";
其中 it
是引用测试属性的关键字。我只是想知道似乎没有编程语言实现了这一点。
Upate:
好的,与此同时我发现了一些有点短的东西,但只能在 JavaScript 中运行:
(function(s){
if(s.display=='block')
s.display="none";
else
s.display='block';
})(document.getElementById("toggledDiv").style)
I've been wondering myself multiple times if, and if not, why not, there is an idiom/shortcut for the following pseudocode:
if object.value == some_value then object.value = some_other_value
For example, in JavaScript I sometimes write:
if (document.getElementById("toggledDiv").style.display == "block") {
document.getElementById("toggledDiv").style.display = "none";
}
This seems to be rather tedious. Is there a name for this idiom, and is there a more concise syntax for this in common programming languages?
Thank you!
Edit: To be more precise, I don't care about the braces, but about that you have to reference the attribute at least to times. I'd like to have something like that (pseudocode):
test ( object.value ):
if (it > 0) it = 0;
else it -= 1;
e. g.:
test (document.getElementById("toggledDiv").style.display):
if (it == "block") it = "none";
where it
is a keyword that references the tested property. I'm just wondering no programming language seems to have implemented that.
Upate:
Okay, in the meantime I have found something which is a little bit short, but only works in JavaScript:
(function(s){
if(s.display=='block')
s.display="none";
else
s.display='block';
})(document.getElementById("toggledDiv").style)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,在 Haskell 和其他 FP 语言中,条件语句(如三元运算符)是一流的表达式,因此您可以将赋值浮出,
使代码更加简洁。
Well, in Haskell, and other FP languages, conditionals, like ternary operators, are first-class expressions, so you can float the assignment out,
Making the code a lot cleaner.
我不知道有哪些语言支持开箱即用,但有许多语言支持定义新运算符。理论上,您可以编写如下内容(在伪代码中)
假设我的伪代码是正确的,这将允许您执行以下操作:
您还可以有两个版本:
前者将减少切换,而更多...您要求的,我猜。我的代码基于之前关于 css/block/none 的注释中的切换用例,其中切换是更常见的行为。
假设该语言支持它,您也可以编写一个
toggle=
运算符:I don't know of any languages that support it out of the box, but there are a number that support defining new operators. In theory, you could write something like the following (in psuedo-code)
Assuming my psuedo-code is correct, this would allow you to do the following:
You could also two versions:
The former would be less of a toggle and more of... what you asked for, I guess. I was basing the code off the toggle use case from the previous note about css/block/none, where toggle is the more common behavior.
Assuming the language supports it, you could write a
toggle=
operator too:在 JavaScript 中你可以这样做:
In JavaScript you can do: