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

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

问题描述

我想知道 angular 指令 中的恒等 === 和相等 == 运算符之间是否有相同的区别,就像在纯 javascript 中一样?

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 相同.另一方面,在 angular 源中,他们只使用相等性检查,即使在 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?

推荐答案

=="和==="运算符的主要区别在于前者通过类型更正来比较变量,例如如果将数字与带有数字文字的字符串进行比较,== 允许这样做,但 === 不允许这样做,因为它不仅检查值,而且检查两个变量的类型,如果两个变量的类型不同"=== 返回假,而=="返回真.

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) 当我们比较两个不同类型的变量时,例如带有字符串的布尔值或带有字符串的数字使用 == 运算符,它会根据内容相等性自动将一种类型转换为另一种类型并返回值,而 === 运算符是 Java 中的严格相等运算符,并且仅当两个变量都为相同的类型,也包含相同的值.通过以下 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) "==" 运算符被称为类型强制运算符,如果两个值相同并使用 == 运算符进行比较,则任何时候都会发生类型强制.另一方面, === 被称为严格相等运算符.它与 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天全站免登陆