sass:相等运算符有不一致的行为,这是一个错误吗?

sass: equality operators have inconsistent behavior, is it a bug?

勾选这个功能

@function mathOperation($number, $type) {
@if $type == sum or $type == rest {
    @return true
} @else {
        @return false
    }
}
@debug mathOperation(5, sum);
@debug mathOperation(5, rest);
@debug mathOperation(5, division);

正如预期的那样,结果是这样的

调试:正确

调试:正确

调试:错误

但是如果我们将这个函数中的相等运算符修改为!=

@function mathOperation2($number, $type) {
    @if $type != sum or $type != rest {
        @return true
    } @else {
        @return false
    }
}

@debug mathOperation2(5, sum);
@debug mathOperation2(5, rest);
@debug mathOperation2(5, division);

结果变得奇怪

调试:正确

调试:正确

调试:正确

我错过了什么吗?这种行为是错误吗?我正在使用 grunt-sass 2.1.0

进行编译

对于第二个示例,代码应为:

@function mathOperation2($number, $type) {
    @if $type != sum and $type != rest {
        @return true
    } @else {
        @return false
    }
}

if 语句中的 or 表示如果两个条件之一为真,则条件将被接受。您应该使用 and,这样如果两个语句都为真,则条件为真。

非常感谢您的解答,我最近5天没睡觉(儿子在医院)我用一张图解决了:

@function map-select-key($map, $key, $direction) {
    $direction-map: (previous: -1, next: 1);
    @if map-has-key($direction-map, $direction) == false {
        @warn '#{$direction} is not an accepted parameter, the only 
parameters accepted for $direction are "#{map-keys($direction-map)}"';
    @return null;
} @else if (map-index-from-key($map, $key) == 1) and ($direction == previous) or (map-index-from-key($map, $key) == length($map)) and ($direction == next) {
    @warn 'it is not possible to select the #{$direction} item: #{$key} is the #{if($direction == previous, first, last)} element on the map #{$map}';
    @return null;
} @else {
    @return map-key-from-index($map, map-index-from-key($map, $key) + map-get($direction-map, $direction));
    }
}