From 310fd066e91f454b990372ffa30e803cc8120975 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 12:56:40 +0100 Subject: unslug zh-cn: move --- .../operators/comparison_operators/index.html | 278 --------------------- 1 file changed, 278 deletions(-) delete mode 100644 files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html (limited to 'files/zh-cn/web/javascript/reference/operators/comparison_operators') diff --git a/files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html b/files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html deleted file mode 100644 index 5ddf85f426..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/comparison_operators/index.html +++ /dev/null @@ -1,278 +0,0 @@ ---- -title: 比较操作符 -slug: Web/JavaScript/Reference/Operators/Comparison_Operators -tags: - - 严格比较操作符 - - 比较操作符 -translation_of: Web/JavaScript/Reference/Operators -translation_of_original: Web/JavaScript/Reference/Operators/Comparison_Operators ---- -
{{jsSidebar("Operators")}}
- -

JavaScript 有两种比较方式:严格比较运算符和转换类型比较运算符。对于严格比较运算符(===)来说,仅当两个操作数的类型相同且值相等为 true,而对于被广泛使用的比较运算符(==)来说,会在进行比较之前,将两个操作数转换成相同的类型。对于关系运算符(比如 <=)来说,会先将操作数转为原始值,使它们类型相同,再进行比较运算。

- -

字符串比较则是使用基于标准字典的 Unicode 值来进行比较的。

- -

比较的特点:

- - - -

相等运算符

- -

相等(==)

- -

比较操作符会为两个不同类型的操作数转换类型,然后进行严格比较。当两个操作数都是对象时,JavaScript会比较其内部引用,当且仅当他们的引用指向内存中的相同对象(区域)时才相等,即他们在栈内存中的引用地址相同。

- -

语法

- -
x == y
-
- -

例子

- -
 1   ==  1     // true
-"1"  ==  1     // true
- 1   == '1'    // true
- 0   == false  // true
-
- -

不相等 (!=)

- -

不等操作符仅当操作数不相等时返回true,如果两操作数不是同一类型,JavaScript会尝试将其转为一个合适的类型,然后进行比较。如果两操作数为对象类型,JavaScript会比较其内部引用地址,仅当他们在内存中引用不同对象时不相等。

- -

语法

- -
x != y
- -

例子

- -
1 !=   2     // true
-1 !=  "1"    // false
-1 !=  '1'    // false
-1 !=  true   // false
-0 !=  false  // false
-
- -

一致/严格相等 (===)

- -

一致运算符不会进行类型转换,仅当操作数严格相等时返回true

- -

语法

- -
x === y
- -

例子

- -
3 === 3   // true
-3 === '3' // false
-var object1 = {"value":"key"}, object2={"value":"key"};
-object1 === object2 //false
- -

不一致/严格不相等 (!==)

- -

不一致运算符当操作数不相等或不同类型时返回true

- -

语法

- -
x !== y
- -

例子

- -
3 !== '3' // true
-4 !== 3   // true
-
- -

关系运算符

- -

大于运算符 (>)

- -

大于运算符仅当左操作数大于右操作数时返回true

- -

语法

- -
x > y
- -

例子

- -
4 > 3 // true
-
- -

大于等于运算符 (>=)

- -

大于等于运算符当左操作数大于或等于右操作数时返回true

- -

语法

- -
 x >= y
- -

例子

- -
4 >= 3 // true
-3 >= 3 // true
-
- -

小于运算符 (<)

- -

小于运算符仅当左操作数小于右操作数时返回true

- -

语法

- -
 x < y
- -

例子

- -
3 < 4 // true
-
- -

小于等于运算符 (<=)

- -

小于等于运算符当左操作数小于或等于右操作数时返回true

- -

语法

- -
 x <= y
- -

例子

- -
3 <= 4 // true
-
- -

使用比较操作符

- -

标准相等操作符(== and !=) 使用 Abstract Equality Comparison Algorithm 去比较两个操作数。当两个操作数类型不相等时,会在比较前尝试将其转换为相同类型。 e.g., 对于表达式 5 == '5', 在比较前会先将右边字符串类型的操作数 5 转换为数字。

- -

严格相等操作符 (=== and !==) 使用 Strict Equality Comparison Algorithm 并尝试对两个相同操作数进行相等比较,如果它们的类型不相等,那么永远会返回false 所以 5 !== '5'。

- -

当需要明确操作数的类型和值的时候,或者操作数的确切类型非常重要时,应使用严格相等操作符。否则,当你允许操作数在比较前进行类型转换时,可以使用标准相等操作符来比较。

- -

当比较运算涉及类型转换时 (i.e., non–strict comparison), JavaScript 会按以下规则对字符串,数字,布尔或对象类型的操作数进行操作:

- - - -
注意: 字符串对象的类型是对象,不是字符串!字符串对象很少被使用,所以下面的结果也许会让你惊讶:
- -
// true as both operands are Type String (i.e. string primitives):
-'foo' === 'foo'
-
-var a = new String('foo');
-var b = new String('foo');
-
-// false as a and b are Type Object and reference different objects
-a == b
-
-// false as a and b are Type Object and reference different objects
-a === b
-
-// true as a and 'foo' are of different type and, the Object (a)
-// is converted to String 'foo' before comparison
-a == 'foo' 
- -

Specifications

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
ECMAScript 1st Edition.StandardInitial definition. Implemented in JavaScript 1.0
ECMAScript 3rd Edition.StandardAdds === and !== operators. Implemented in JavaScript 1.3
{{SpecName('ES5.1', '#sec-11.8', 'Relational Operators')}}
- {{SpecName('ES5.1', '#sec-11.9', 'Equality Operators')}}
{{Spec2('ES5.1')}}Defined in several sections of the specification: Relational OperatorsEquality Operators
{{SpecName('ES6', '#sec-relational-operators', 'Relational Operators')}}
- {{SpecName('ES6', '#sec-equality-operators', 'Equality Operators')}}
{{Spec2('ES6')}}Defined in several sections of the specification: Relational OperatorsEquality Operators
{{SpecName('ESDraft', '#sec-relational-operators')}}{{Spec2('ESDraft')}}Defined in several sections of the specification: Relational OperatorsEquality Operators
- -

Browser compatibility

- -

{{CompatibilityTable}}

- -
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -
- - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
-
- -

See also

- - -- cgit v1.2.3-54-g00ecf