值与判断
先记住几组「长得不像」的基本值。后面所有 if 都靠它们。
真假与空
| Python | JavaScript | 说明 |
|---|---|---|
True / False | true / false | Python 首字母大写 |
None | null / undefined | Python 只有一个空值 |
and / or / not | && / || / ! | 关键字,不是符号 |
if not text: | if (!text) | 空串、空列表、None、0 都偏假 |
x is None | x === null | 身份判断用 is,不要写 == None |
a or b | a || b | 取第一个真值,常当缺省回退 |
python
if not text:
return ""
date_compact = date_str or datetime.now().strftime("%Y%m%d")
content_raw = item["content"] or item["description"]js
if (!text) return ''
const dateCompact = date_str || formatToday()
const contentRaw = item.content || item.description多值「在不在」
python
if normalized.get(field) in (None, ""):
...js
if ([null, undefined, ''].includes(normalized[field])) {
...
}| Python | JavaScript |
|---|---|
x in (a, b, c) | [a, b, c].includes(x) |
x in {"a", "b"} | set.has(x) |
下一页叠:怎么写函数、怎么导入。