-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
702 additions
and
610 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### 原理 | ||
|
||
Object.is() 判断两个值是否相同. 如果下列任何一项成立, 则两个值相同: | ||
|
||
- 两个值都是 undefined | ||
- 两个值都是 null | ||
- 两个值都是 true 或者都是 false | ||
- 两个值是由相同个数的字符按照相同的顺序组成的字符串 | ||
- 两个值指向同一个对象 | ||
- 两个值都是数字并且 | ||
- 都是正零 +0 | ||
- 都是负零 -0 | ||
- 都是 NaN | ||
- 都是除零和 NaN 外的其它同一个数字 | ||
|
||
这种相等性判断逻辑和传统的 `==` 运算不同, `==` 运算符会对它两边的操作数做隐式类型转换(如果它们类型不同), 然后才进行相等性比较, (所以才会有类似 `""` `==` `false` 等于 `true` 的现象), 但 `Object.is` 不会做这种类型转换. | ||
|
||
这与 `===` 运算符的判定方式也不一样. `===` 运算符(和`==` 运算符)将数字值 `-0` 和 `+0` 视为相等, 并认为 `Number.NaN` 不等于 `NaN`. | ||
|
||
### 参考资料 | ||
|
||
[Object is](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/is) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* @Author: Rainy | ||
* @Date: 2019-11-14 19:25:01 | ||
* @LastEditors: Rainy | ||
* @LastEditTime: 2019-12-02 20:28:56 | ||
*/ | ||
|
||
import { _is } from '.'; | ||
|
||
test(`is('foo', 'foo') should true`, () => { | ||
expect(_is('foo', 'foo')).toBe(true); | ||
}); | ||
|
||
test(`is([], []) should false`, () => { | ||
expect(_is([], [])).toBe(false); | ||
}); | ||
|
||
const u1 = { name: 'Rain120' }; | ||
const u2 = { name: 'Rain120' }; | ||
|
||
test(`is(u1, u1) should true`, () => { | ||
expect(_is(u1, u1)).toBe(true); | ||
}); | ||
|
||
test(`is(u1, u2) should false`, () => { | ||
expect(_is(u1, u2)).toBe(false); | ||
}); | ||
|
||
test(`is(null, null) should true`, () => { | ||
expect(_is(null, null)).toBe(true); | ||
}); | ||
|
||
test(`is(0, -0) should false`, () => { | ||
expect(_is(0, -0)).toBe(false); | ||
}); | ||
|
||
test(`is(0, +0) should true`, () => { | ||
expect(_is(0, +0)).toBe(true); | ||
}); | ||
|
||
|
||
test(`is(NaN, 0 / 0) should true`, () => { | ||
expect(_is(NaN, 0 / 0)).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* @Author: Rainy | ||
* @Date: 2019-11-14 19:25:01 | ||
* @LastEditors: Rainy | ||
* @LastEditTime: 2019-12-02 20:32:37 | ||
*/ | ||
export function _is(source: any, target: any): boolean { | ||
if (source === target) { | ||
return source !== 0 || 1 / source === 1 / target; | ||
} else { | ||
return source !== source && target !== target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,8 @@ | |
|
||
- [ ] Object | ||
|
||
- [x] is | ||
|
||
- [ ] ES6 | ||
|
||
- [ ] Promise | ||
|