【问题标题】:sass: equality operators have inconsistent behavior, is it a bug?sass:相等运算符的行为不一致,这是一个错误吗?
【发布时间】:2018-04-11 21:52:16
【问题描述】:

检查此功能

@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 进行编译

【问题讨论】:

  • 第二个例子中的条件永远为真,因为$type不能同时是两个值。

标签: function sass operator-keyword equality


【解决方案1】:

第二个例子的代码应该是:

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

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

【讨论】:

    【解决方案2】:

    非常感谢各位的解答,最近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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2013-01-17
      • 2014-12-26
      • 2016-06-26
      相关资源
      最近更新 更多