跳至內容
在此頁面

內省 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";
}