or 和 || 的区别设置变量时 [英] Difference between or and || when setting variables

查看:30
本文介绍了or 和 || 的区别设置变量时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是 ||or 是同义词.

I was under the impression that || and or were synonymous.

or设置变量不保值;为什么?

Setting variable with or does not hold value; why?

>> test = nil or true
=> true
>> test
=> nil

>> test = false or true
=> true
>> test
=> false

使用 ||

>> test = nil || true
=> true
>> test
=> true

推荐答案

or= 具有更低的优先级.

test = nil or true

(test = nil) or true

这是true,同时将test设置为nil.

|| 的优先级高于 =.

test = nil || true

test = (nil || true)

这是true,同时将test设置为true.

这篇关于or 和 || 的区别设置变量时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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