函数值
- Dart Sass
- ✓
- LibSass
- since 3.5.0
- Ruby Sass
- since 3.5.0
In older versions of LibSass and Ruby Sass, the call()
function took a
string representing a function’s name. This was changed to take a function
value instead in preparation for a new module system where functions are no
longer global and so a given name may not always refer to the same function.
Passing a string to call()
still works in all implementations, but it’s
deprecated and will be disallowed in future versions.
函数 也可以是值!你不能直接将函数编写为值,但可以将函数名称传递给 meta.get-function()
函数 以将其作为值获取。一旦获得函数值,就可以将其传递给 meta.call()
函数 来调用它。这对于编写调用其他函数的高阶函数很有用。
¥Functions can be values too! You can’t directly write a function as a value,
but you can pass a function’s name to the meta.get-function()
function to
get it as a value. Once you have a function value, you can pass it to the
meta.call()
function to call it. This is useful for writing higher-order
functions that call other functions.
SCSS Syntax
@use "sass:list";
@use "sass:meta";
@use "sass:string";
/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition) {
$new-list: ();
$separator: list.separator($list);
@each $element in $list {
@if not meta.call($condition, $element) {
$new-list: list.append($new-list, $element, $separator: $separator);
}
}
@return $new-list;
}
$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;
.content {
@function contains-helvetica($string) {
@return string.index($string, "Helvetica");
}
font-family: remove-where($fonts, meta.get-function("contains-helvetica"));
}
Sass Syntax
@use "sass:list"
@use "sass:meta"
@use "sass:string"
/// Return a copy of $list with all elements for which $condition returns `true`
/// removed.
@function remove-where($list, $condition)
$new-list: ()
$separator: list.separator($list)
@each $element in $list
@if not meta.call($condition, $element)
$new-list: list.append($new-list, $element, $separator: $separator)
@return $new-list
$fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif
.content
@function contains-helvetica($string)
@return string.index($string, "Helvetica")
font-family: remove-where($fonts, meta.get-function("contains-helvetica"))
CSS Output
.content {
font-family: Tahoma, Geneva, Arial, sans-serif;
}