在我SCSS腳本之一,我發現我不小心給一個@for
循環計數器相同的名稱不同,全局變量,但如預期一切仍然工作。變量範圍的for循環
例如,粘貼示例腳本到http://sassmeister.com/:
$w: white;
$r: red;
$b: blue;
$y: yellow;
//...
$test: '';
//Accidentally using an existing variable name ($r) as the counter:
@for $r from 1 through 10 {
@if($test != '') { $test: $test + ', '; }
$test: $test + $r;
}
.someclass {
/*Note: $r is 'red', not the @for counter. @for loops create their own scope?*/
color: $r;
/*All the @for counters. @for created a *local* $r, but accessed the *global* $test...*/
something: unquote($test);
}
...和CSS的樣子:
.someclass {
/*Note: $r is 'red', not the @for counter. @for loops create their own scope?*/
color: red;
/*All the @for counters. @for created a *local* $r, but accessed the *global* $test...*/
something: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;
}
所以,我會認爲@for
循環改變$r
從red
到10
到它在.someclass
中使用的時間,但(幸運的是)它沒有。這表示該循環在其自己的本地範圍內工作,但它仍然訪問全局變量$test
,即使不使用!global
標誌。
現在我很困惑。 docs指出mixin具有局部範圍,但我還沒有找到任何關於循環範圍的文檔。 這是「混合」範圍的文檔化功能 - 循環計數器是在一些本地範圍內,而循環「身體」在父範圍內工作 - 或者它是SCSS編譯器中的錯誤?
你在這裏似乎沒有實際問題。唯一能回答「這是一個錯誤」的人是Sass的維護者。 – cimmanon
@cimmanon - 好吧,你永遠不會知道。有些人比我更瞭解薩斯方式的內部運作(方式)。無論如何,我現在已經在他們的github頁面上提交了一個問題。 – Sphinxxx