+!! operator in an if statement

后端 未结 6 2017
旧巷少年郎
旧巷少年郎 2020-12-29 18:31

I was reviewing the code for an angularjs factory to better understand how it works. The code contains an if statement that I don\'t fully understand.

I

6条回答
  •  悲&欢浪女
    2020-12-29 19:13

    if ((+!!config.template) + (+!!config.templateUrl) !== 1) {
    
                0            +         0               !== 1  true
                0            +         1               !== 1  false
                1            +         0               !== 1  false
                1            +         1               !== 1  true
    

    is equal to

    if (!config.template === !config.templateUrl) {
    

    despite the content of the two properties.

提交回复
热议问题