gpt4 book ai didi

javascript - try() activesupport 方法的 coffeescript 模拟

转载 作者:搜寻专家 更新时间:2023-11-01 05:00:25 25 4
gpt4 key购买 nike

有一个非常有用的 .try() rails 方法,它对我这样的函数有很大帮助:

hash.try(:a).try(:b)
# equal to
# if hash.present? && hash.a.present?
# hash.a.b
# else
# nil
# end

coffeescript中有类似的东西吗?

最佳答案

是的:

hash?.a?.b

参见 the doc 中的存在运算符

关于javascript - try() activesupport 方法的 coffeescript 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30505571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com