Stylus支持自我檢測的API, 這允許混寫以及函數(shù)反應(yīng)調(diào)用者的相關(guān)性。
mixin這個(gè)局部變量在函數(shù)體內(nèi)自動(dòng)賦值。如果調(diào)用的函數(shù)在根級(jí)別,則mixin包含字符串root, 如果其他情況,則是block, 如果調(diào)用函數(shù)有返回值,最終為false.
下面這個(gè)例子中,我們定義reset(), 根據(jù)其是混入了根部,還是混入塊狀域,還是混入返回值中,來修改其值,并作為foo屬性的值呈現(xiàn):
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"; }
更多建議: