@function foo($x) {
@return $x;
}
h1 {
@if (function-exists('get-function')) {
content: call(get-function('foo'), 'Displays in Sass 3.5.0 and up');
} @else {
content: call('foo', 'Displays in Sass 3.3.x and 3.4.x');
}
}
// Unfortunately assigning a function to a variable
// can't be polyfilled for Sass 3.3.x and 3.4.x
$foo: get-function(foo);
h1 { content: call($foo, 'bar'); }
// -> el { content: get-function(foo)("bar"); }
使用safe-get-function
我合并了一个 safe-get-function 的工具,在Sass旧版本和未来版本中都有效,所以你现在就可以在代码中使用get-function,为Sass 3.5和4.0做好准备。
// Simplified version of safe-get-function
// Full code: https://github.com/kaelig/sass-safe-get-function
@function safe-get-function($name) {
@if function-exists('get-function') {
@return get-function($name);
} @else {
@return $name;
}
}
@function foo($x) { @return $x; }
h1 {
content: call(safe-get-function('foo'), 'Works in all versions of Sass!');
}
safe-get-function()现可在npm上使用: