Ole*_*Ole 8 javascript rxjs typescript angular rxjs6
我正在用代码段阅读一些代码:
search(query: string) {
of(query).
pipe(
filter(Boolean),
debounceTime(300),
Run Code Online (Sandbox Code Playgroud)
是filter(Boolean)基本一样的东西filter(v=>!!v)?
Cor*_*nie 15
它们实现了相同的结果,因为您不会在订阅中获得未定义的值。
不同之处在于您在使用 filter(Boolean) 时会丢失类型推断
const query = 'the query';
of(query).
pipe(
filter(Boolean)
).subscribe(val); // val here is of type 'Any'
of(query).
pipe(
filter(Boolean)
).subscribe((val: string)); // we can infer it back to string later
of(query).
pipe(
filter(v=> v!== undefined)
).subscribe(val); // val here is of type 'string'
Run Code Online (Sandbox Code Playgroud)
是的,它们是相同的。
console.log(typeof Boolean); // prints function
console.log(Boolean.prototype.constructor("truthy")); // prints true
console.log(Boolean === Boolean.prototype.constructor); // prints true
Run Code Online (Sandbox Code Playgroud)
在Boolean全球参考点,以返回从第一个参数一个布尔值的构造函数。
构造函数可用于创建布尔包装对象,但它与原始的true值不同。
console.log(new Boolean("truthy")); // prints an object.
console.log(new Boolean("truthy").valueOf() === true); // prints true
console.log((new Boolean("truthy")) === true); // prints false
console.log(Boolean("truthy") === true); // prints true
Run Code Online (Sandbox Code Playgroud)
参考:https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Boolean
| 归档时间: |
|
| 查看次数: |
945 次 |
| 最近记录: |