Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于JS的for...of循环 #1

Open
yurensmile opened this issue Apr 1, 2019 · 2 comments
Open

关于JS的for...of循环 #1

yurensmile opened this issue Apr 1, 2019 · 2 comments
Labels

Comments

@yurensmile
Copy link
Owner

yurensmile commented Apr 1, 2019

for...of

for...of循环用来对一个可迭代对象进行迭代,可迭代对象是指实现了@@iterator方法,也就是该对象拥有Symbol.iterator属性。

哪些对象实现了@@iterator方法呢

JS的一些内置对象已经实现了@@iterator方法,比如Array、Map、Set、String(注意哦,Object是没有的)
当然我们也可以自定义实现@@iterator方法,

let myIterator = {};
myIterator[Symbol.iterator]=function* (){
    let i = 1;
    while(i,3){
        yield i++
    }
}

和for...in有什么区别呢

首先要明确一点,对象的属性是有特性的,这些特性用来描述属性,比如[[enumerable]]/[[Writable]],具体的可以看这里,与for...in循环有关的就是这个[[enumerable]]特性,它表示这个属性是否是可遍历的(boolean值), for...in 循环遍历的就是[[enumerable]]特性值为真的属性。
for...of循环则是要求对象实现@@iterator方法,它迭代的值是这个方法所产生的。

@yurensmile
Copy link
Owner Author

yurensmile commented Apr 1, 2019

插个楼:大屏是让用户看到更多的内容而不是更大的内容<rem与vw>

@yurensmile yurensmile added the js label Apr 17, 2019
@yurensmile
Copy link
Owner Author

Object.keys(obj) --> 自身的可循环的
Object.getOwnPropertyNames(obj) --> 自身的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant