自省 API
约 201 字小于 1 分钟
2025-04-22
Stylus 支持自省 API。这使得混入 (mixin) 和函数能够相对于调用者进行反射等操作。
mixin
mixin
局部变量在函数体内自动分配。如果函数是在根级别调用的,则它包含字符串 root
,否则包含 block
,如果调用的函数需要返回值,则最后包含 false
。
在下面的示例中,我们定义 reset()
来根据它是否混入到根节点、另一个块或返回值中来改变其行为,symbol_str
为一个无特殊意义的字符串,如下面的 foo
属性中所使用的:
reset()
if mixin == 'root'
symbol_str
root true
else if mixin
symbol_str 'a mixin'
else
'not a mixin'
reset()
body
reset()
foo reset()
编译为:
symbol_str {
root: true;
}
body {
foo: "not a mixin";
symbol_str: "a mixin";
}
更新日志
2025/8/24 08:17
查看所有更新日志
e7112
-1于