编程语言惯用法“if object.value == some_value then object.value = some_other_value”

发布于 2024-11-14 22:11:45 字数 1005 浏览 3 评论 0原文

我多次想知道自己是否有,如果没有,为什么不存在以下伪代码的惯用语/快捷方式:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揪着可爱 2024-11-21 22:11:45

嗯,在 Haskell 和其他 FP 语言中,条件语句(如三元运算符)是一流的表达式,因此您可以将赋值浮出,

a = if x == y then x else z

使代码更加简洁。

Well, in Haskell, and other FP languages, conditionals, like ternary operators, are first-class expressions, so you can float the assignment out,

a = if x == y then x else z

Making the code a lot cleaner.

悸初 2024-11-21 22:11:45

我不知道有哪些语言支持开箱即用,但有许多语言支持定义新运算符。理论上,您可以编写如下内容(在伪代码中)

operator <T> T toggle(T value, T[] values) {
    for(int i=0; i<values.size(); i++) {
        if(value == values[i]) {
            if(values.size() > (i+1)) {
                return values[i+1]
            } else {
                return values[0]

            }
         }
    }
    error "value $value not found in value list $values"
}

假设我的伪代码是正确的,这将允许您执行以下操作:

v = true;
v = v toggle [true, false] ;       // v == false
v = v toggle [true, false] ;       // v == true (loops to beginning of list
v = v toggle [false, true, true] ; // v == true, since true is both the 2nd and 3rd elements of the list

您还可以有两个版本:

  • 一个是,如果值不在列表,返回原始值
  • 如果值不在列表中,则会抛出错误(我的版本做了什么)

前者将减少切换,而更多...您要求的,我猜。我的代码基于之前关于 css/block/none 的注释中的切换用例,其中切换是更常见的行为。

假设该语言支持它,您也可以编写一个 toggle= 运算符:

v toggle= ['none', 'block']

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)

operator <T> T toggle(T value, T[] values) {
    for(int i=0; i<values.size(); i++) {
        if(value == values[i]) {
            if(values.size() > (i+1)) {
                return values[i+1]
            } else {
                return values[0]

            }
         }
    }
    error "value $value not found in value list $values"
}

Assuming my psuedo-code is correct, this would allow you to do the following:

v = true;
v = v toggle [true, false] ;       // v == false
v = v toggle [true, false] ;       // v == true (loops to beginning of list
v = v toggle [false, true, true] ; // v == true, since true is both the 2nd and 3rd elements of the list

You could also two versions:

  • One that, if the values isn't in the list, returns the original value
  • One that, if the values isn't in the list, throws an error (what my version did)

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:

v toggle= ['none', 'block']
听,心雨的声音 2024-11-21 22:11:45

在 JavaScript 中你可以这样做:

var d = document.getElementById("toggledDiv");
if (d.style.display == "block") d.style.display = "none";

In JavaScript you can do:

var d = document.getElementById("toggledDiv");
if (d.style.display == "block") d.style.display = "none";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文