遇到了极其诡异的不符合进入 if 条件的要求但是还是进入了

109 天前
 zxkmm

代码

    addFrontLine(_line_location_, _padding_, _border_) {

        console.log(_padding_);
        console.log(_line_location_);

        if (_padding_ >= _line_location_) {

            _padding_ = _line_location_

        }

        console.log(_padding_);
        console.log(_line_location_);

        const css = `

        .layout-tab-container .b3-list-item > .b3-list-item__toggle {
            padding-left: 4px !important;
        }

        .layout-tab-container ul ul:before {
            content: "";
            position: absolute;
            top: 0;
            bottom: 0;
            left: ${_line_location_}px;
            border-left: ${_border_}px solid var(--b3-theme-background-light);
        }
        
        .layout-tab-container ul ul {
            position: relative;
            padding-left: ${_padding_}px;
        }
        
        `
        this.applyStyles(css);
    }

执行后控制台输出的值

6
20
20
20

如果把 if 语句注释掉,就恢复正常了。 有没有大佬帮看看是什么原因? 完整代码 https://github.com/zxkmm/siyuan_doctree_compress/blob/13f1b3cda6c721001c1c33ab449e6321bc791807/src/index.ts#L118

989 次点击
所在节点    TypeScript
3 条回复
finab
109 天前
你打印下变量类型,如果 if 走了,那肯定不是数值类型,说不定是俩字符串呢
zxkmm
109 天前
@finab 感谢,问题找到了,我好笨
zxkmm
109 天前
见谅见谅,第一次写动态类型的语言

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1011000

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX