简体   繁体   English

类型检查运算符语句的结果

[英]Type-checking the result of operator statements

a = 'A';
b = null;

const w = a === b;
const x = a && a.length;
const y = b && b.length;
const z = (a && a.length) || (b && b.length);
const u = (a && a.length) && (b && b.length);

console.log(typeof w); // boolean
console.log(typeof x); // number 
console.log(typeof y); // object
console.log(typeof z); // number 
console.log(typeof u); // object

I was expecting all of them to be boolean?我期待他们都是boolean? Can you please help me understand why some of them are not boolean ?你能帮我理解为什么其中一些不是 boolean 吗?

It is not obvious to me why short-circuit evaluation results in different z and u types.我不清楚为什么短路评估会导致不同的 z 和 u 类型。

x, y, z, u are short circuit evaluations and not conditions. x, y, z, u是短路评估,而不是条件。

const x = a && a.length;

What that means is assign a.length to x is a exists and so on.这意味着将a.length分配给x is a exists 等等。 Hence a number .因此有一个number

Whereas if you were to put them inside an if condition they would be implicitly type casted to boolean而如果您将它们放在 if 条件中,它们将被隐式类型转换为boolean

 const a = null || "works." console.log(a)

If you run the above snippet you will realise how ||如果你运行上面的代码片段,你会意识到|| works.作品。 If the value on the left of ||如果 || 左边的值evaluates to false then value on right is returned otherwise left value.计算结果为假,则返回右边的值,否则返回左边的值。

 const x = a && a.length; // a.length = 1 const y = b && b.length; // b = null const z = (a && a.length) || (b && b.length); // a.length = 1 const u = (a && a.length) && (b && b.length); // b = null a && a.length // ===> true and 1 b && b.length // ===> false and next line;

x || y x || y if x is true return x and y not exec, if x is false return y; x || y如果 x 为真,则返回 x 并且 y 不执行,如果 x 为假,则返回 y;

x && y if x is true return y, if x is false return x; x && y如果 x 为真,则返回 y,如果 x 为假,则返回 x;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:确保参数产生有效的HTML元素(类型检查) - Javascript: Ensuring an argument yields a valid HTML element (type-checking) 将流类型检查添加到grunt服务器开发工作流 - Adding flow type-checking to grunt server development workflow 在返回的对象中类型检查正确的键值使用 - Type-checking correct key-value usage in a returned object 大型 SPA 前端中的静态与动态类型检查? - Static vs dynamic type-checking in a large SPA frontend? 如何将方法添加到分配给变量c的对象常量中,以便满足类型检查? - How to add methods to the object literal being assigned to the variable c, so that the type-checking is satisfied? TypeScript 在使用它进行类型检查时出错 JavaScript 代码包含 React 组件 - TypeScript errors while using it for type-checking JavaScript code containing React components 如果JS包装器对象被认为是不良样式,是否使用lodash _.isString等进行类型检查也被视为不良样式? - if JS wrapper objects are considered bad style, is type-checking with lodash _.isString etc. considered bad style as well? 在 Javascript 中是否仅使用 typeof 运算符检查类型不好? - In Javascript is checking for the type only using typeof operator bad? 错误:MIME类型('application / json')无法执行,并且使用YQL语句启用了严格的MIME类型检查? - Error: MIME type ('application/json') is not executable, and strict MIME type checking is enabled with YQL statements? JavaScript不检查逻辑语句 - JavaScript not checking logical statements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM