跳至内容
本页内容

内省 API

Stylus 支持内省 API。这允许 mixin 和函数相对于调用者进行反射等。

mixin

mixin 局部变量会在函数体中自动分配。如果函数在根级别被调用,则它包含字符串 root,否则表示 block,最后如果被调用的函数需要返回值,则表示 false

在以下示例中,我们定义 reset() 来改变它的行为,具体取决于它是混合到根、另一个块还是返回值中,如下面 foo 属性中所使用的那样

reset()
  if mixin == 'root'
    got
      root true
  else if mixin
    got 'a mixin'
  else
    'not a mixin'

reset()

body
  reset()
  foo reset()
reset()
  if mixin == 'root'
    got
      root true
  else if mixin
    got 'a mixin'
  else
    'not a mixin'

reset()

body
  reset()
  foo reset()

编译为

got {
  root: true;
}
body {
  foo: "not a mixin";
  got: "a mixin";
}
got {
  root: true;
}
body {
  foo: "not a mixin";
  got: "a mixin";
}