sass:等于运算符的行为不一致,是不是一个bug?

时间:2018-04-11 21:52:16

标签: function sass operator-keyword equality

检查此功能

@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);

正如预期的那样结果就是这个

DEBUG:true

DEBUG:true

DEBUG:false

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

@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);

结果变得奇怪

DEBUG:true

DEBUG:true

DEBUG:true

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

2 个答案:

答案 0 :(得分:0)

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

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

or语句中的if表示如果其中一个条件为真,则接受该条件。你应该使用and所以如果两个陈述都是真的那么条件将是tru。

答案 1 :(得分:0)

非常感谢您的回答,我在最近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));
    }
}