Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-study-front-share
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
CI / CD
CI / CD
Pipelines
Schedules
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
yifu-study
front
yifu-study-front-share
Commits
5a927fd9
Commit
5a927fd9
authored
Apr 26, 2022
by
zhangfangli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
map和object
parent
f4a065ec
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
3 deletions
+6
-3
Map 和 Object.md
source/_posts/Map 和 Object.md
+6
-3
No files found.
source/_posts/Map 和 Object.md
View file @
5a927fd9
...
...
@@ -266,15 +266,18 @@ Objects 和 Maps 类似的是,它们都允许你按键存取一个值、删除
|
**next**
| 一个无参数或者一个参数的函数,返回一个应当拥有以下两个属性的对象:
<br
/>
**done(boolean)**
<br
/>
如果迭代器可以产生序列中的下一个值,则为 false。
<br
/>
如果迭代器已将序列迭代完毕,则为 true。
<br
/>
这种情况下,value 是可选的,如果它依然存在,即为迭代结束之后的默认返回值。
<br
/>
**next()**
方法必须返回一个对象,
<br
/>
该对象应当有两个属性: done 和 value,如果返回了一个非对象值(比如 false 或 undefined),
<br
/>
则会抛出一个
[
TypeError
](
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/TypeError
)
异常("iterator.next() returned a non-object value")。 |
Iterator 迭代器是一种接口,为不同的数据结构提供统一的访问机制,这个访问机制主要是遍历,我们知道,在数组、在对象、在类数组、在map、在set里面,都可以用for of或者扩展运算符来得到一个数组或者是遍历当前的数据结构,为什么能够遍历的原因就是因为存在这个Iterator 迭代器这个东西,所以我们可以用for of来提供一个统一的遍历,因此这个底层或者是Itrator它最初是为了for of而设计的。
Iterator 迭代器是一种接口,为不同的数据结构提供统一的访问机制,这个访问机制主要是遍历,我们知道,在数组、在对象、在类数组、在map、在set里面,都可以用for of或者扩展运算符来得到一个数组或者是遍历当前的数据结构,为什么能够遍历的原因就是因为存在这个Iterator 迭代器这个东西,所以我们可以用for of来提供一个统一的遍历,因此这个底层或者是Itrator它最初是为了for of而设计的。
为了给所有的
[
数据结构
](
https://so.csdn.net/so/search?q=%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84&spm=1001.2101.3001.7020
)
有一个统一的遍历接口,统一的访问机制,因此就产生了迭代器。
<br
/>
迭代器的特点:
-
按某种次序排列数据结构
-
为for of提供一个遍历支持
-
为不同的数据结构提供统一的访问机制(目的)
目前所有的内置可迭代对象如下:
[
String
](
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String
)
、
[
Array
](
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array
)
、
[
TypedArray
](
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
)
、
[
Map
](
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map
)
和
[
Set
](
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set
)
等,它们的原型对象都实现了 @@iterator 方法。对象(Object)之所以没有默认部署Iterator接口,是因为对象的哪个属性先遍历,哪个属性后遍历是不确定的,需要开发者手动指定。
<br
/>
例如:由上面的学习,我们知道map的values() 返回的就是一个Iterator对象
```
javascript
let
map
=
new
Map
([
[
'name'
,
'zz'
],
...
...
@@ -319,9 +322,9 @@ obj[Symbol.iterator] = function() {
};
}
};
}
}
console
.
log
([...
obj
])
// ['张小张', 18]
console
.
log
([...
obj
])
// ['张小张', 18]
```
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment