何时使用此示例?

问题描述 投票:2回答:1

给出以下代码:

if (!!a !== !!b) return false;

[在什么情况下可能需要实施它而不是使用宽松的相等性:

if( a != b ) return false;
javascript boolean-operations
1个回答
0
投票

如以下一些测试案例所示:

  • nullundefined的本机对象不相等
  • 通过toStringvalueOf强制其类型的其他对象取决于测试真实状态时其值是什么

注意:

  • !!不是必需的,因为它被应用于条件测试的两侧;在这种情况下,仅需要将真/伪分别转换为true / false;所以test3和test4是如何写的其他等效示例
  • 为什么Svelte或其他应用程序是这样编写的?如果有意的话,这可能是微观优化,但这需要更多的研究来证实这一假设。

function test1(a,b){
  return !!a !== !!b
}

function test2(a,b){
  return a != b
}

function test3(a,b){
  return !a !== !b
}

function test4(a,b){
  return !(!a == !b)
}

console.log('test1(0,false):', test1(0, false) );
console.log('test2(0,false):', test2(0, false) );
console.log('test3(0,false):', test3(0, false) );
console.log('test4(0,false):', test4(0, false) );
separator()

console.log('test1(\'\',false):', test1('', false) );
console.log('test2(\'\',false):', test2('', false) );
console.log('test3(\'\',false):', test3('', false) );
console.log('test4(\'\',false):', test4('', false) );
separator()

console.log('test1(null,false):', test1(null,false) );
console.log('test2(null,false):', test2(null,false) );
console.log('test3(null,false):', test3(null,false) );
console.log('test4(null,false):', test4(null,false) );
separator()

console.log('test1(undefined,false):', test1(undefined,false) );
console.log('test2(undefined,false):', test2(undefined,false) );
console.log('test3(undefined,false):', test3(undefined,false) );
console.log('test4(undefined,false):', test4(undefined,false) );
separator()

console.log('test1([],false):', test1([],false) );
console.log('test2([],false):', test2([],false) );
console.log('test3([],false):', test3([],false) );
console.log('test4([],false):', test4([],false) );
separator()

console.log('test1([\'a\'],false):', test1(['a'],false) );
console.log('test2([\'a\'],false):', test2(['a'],false) );
console.log('test3([\'a\'],false):', test3(['a'],false) );
console.log('test4([\'a\'],false):', test4(['a'],false) );
separator()

console.log('test1([\'\'],false):', test1([''],false) );
console.log('test2([\'\'],false):', test2([''],false) );
console.log('test3([\'\'],false):', test3([''],false) );
console.log('test4([\'\'],false):', test4([''],false) );
separator()

console.log('test1([0],false):', test1([0],false) );
console.log('test2([0],false):', test2([0],false) );
console.log('test3([0],false):', test3([0],false) );
console.log('test4([0],false):', test4([0],false) );
separator()

console.log('test1([1],false):', test1([1],false) );
console.log('test2([1],false):', test2([1],false) );
console.log('test3([1],false):', test3([1],false) );
console.log('test4([1],false):', test4([1],false) );
separator()

console.log('test1({},false):', test1({},false) );
console.log('test2({},false):', test2({},false) );
console.log('test3({},false):', test3({},false) );
console.log('test4({},false):', test4({},false) );
separator()

console.log('test1({a:1},false):', test1({a:1},false) );
console.log('test2({a:1},false):', test2({a:1},false) );
console.log('test3({a:1},false):', test3({a:1},false) );
console.log('test4({a:1},false):', test4({a:1},false) );

function separator(){console.log('='.repeat(30))}
.as-console-wrapper { 
  height: 100vh !important;  
  max-height: 100vh !important; 
}
© www.soinside.com 2019 - 2024. All rights reserved.