角度条件的工作方式与纯JavaScript相同吗? [英] Do angular conditions work the same way as pure javascript?

查看:115
本文介绍了角度条件的工作方式与纯JavaScript相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 angular指令中的身份===和相等==运算符之间是否有相同的区别?

I wonder if there is the same difference between identity === and equality == operators in angular directives like in pure javascript?

例如

ng-if="value === 'foo'

优于

ng-if="value == 'foo'

我检查的是

ng-if="true == 1

通过但

ng-if="true === 1

没有,所以看起来它的工作方式与纯js相同.另一方面,在角度源中,它们仅使用相等性检查,甚至以js身份为佳.

doesn't, so it looks like it works the same way like pure js. On the other hand in angular source they use just equality check, even tho in js identity is preferred.

https://github .com/angular/angular.js/search?utf8 =%E2%9C%93& q = ng-if

我们应该在角度指令中使用哪个运算符?

Which operator should we use in angular directives?

要澄清-我不是在问JavaScript条件,这已经在堆栈上回答了,我的问题是-纯js和angular指令条件之间的条件是否有区别?

To clarify - I'm not asking about javascript conditions, this has been already answered on stack, my question is - is there any difference in conditions between pure js and angular directive conditions?

推荐答案

"=="和"==="运算符之间的主要区别是前者通过进行类型校正来比较变量,例如如果您将数字与带有数字文字的字符串进行比较,则==允许这样做,但===不允许这样做,因为如果两个变量的类型不同,它不仅会检查值,而且还会检查两个变量的类型" ===返回false,而" ==返回true.

Main difference between "==" and "===" operator is that former compares variable by making type correction e.g. if you compare a number with a string with numeric literal, == allows that, but === doesn't allow that, because it not only checks the value but also type of two variable, if two variables are not of same type "===" return false, while "==" return true.

由于JavaScript同时支持严格相等和类型转换相等,因此了解哪个运算符用于哪个运算很重要.就像我说的那样,===考虑变量的类型,而==根据变量的值进行类型校正,以下是JavaScript编程语言中"=="和"==="运算符之间的更多区别:

Since JavaScript support both strict equality and type-converting equality, it's important to know which operator is used for which operation. As I said that, === takes type of variable in consideration, while == make type correction based upon values of variables, following are couple of more differences between "==" and "===" operator in JavaScript programming language :

1)当我们比较两个不同类型的变量时,例如使用==运算符的带字符串的布尔值或带String的数字的布尔值,它会自动将一种类型转换为另一种类型,并根据内容相等性返回值,而===运算符是Java中的严格相等运算符,并且仅当两个变量都为true时才返回true相同的类型,并且包含相同的值.下面的JavaScript中==和===运算符的示例将非常清楚:

1) When we compare two variables of different type e.g. a boolean with a string or a number with String using == operator, it automatically converts one type into another and return value based upon content equality, while === operator is strict equality operator in Java, and only return true if both variable of same type and also contains same value. This will be much clear with following example of == and === operator in JavaScript :

0==false   // true, because false is equivalent of 0
0===false  // false, because both operands are of different type
2=="2"     // true, auto type coercion, string converted into number
2==="2"    // false, since both operands are not of same type

2)"=="运算符被称为类型强制运算符,并且如果两个值相同并且使用== operator进行比较,则在任何时候都会发生类型强制转换.另一方面,===被称为严格相等运算符.它与Java的相等运算符(==)非常相似,如果您比较两个类型互不兼容的变量,则会产生编译错误.实际上,您应该始终使用"==="运算符来比较变量或仅用于任何比较.

2) "==" operator is known as type coercion operator and anytime if both values are same and compared using ==operator, type coercion happens. On the other hand === is known as strictly equality operator. It's much similar Java's equality operator (==), which gives compilation error if you compare two variables, whose types are not compatible to each other. In fact, you should always use "===" operator for comparing variables or just for any comparison.

这篇关于角度条件的工作方式与纯JavaScript相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆