
您可以在 github 仓库中找到这篇文章中的所有代码。
本地项目发布到GitHub的完整工作流助手。自动执行:安全检查(.gitignore、排除敏感.env/凭据文件)、补齐开源文件(LICENSE、.gitignore)、git init/commit/branch、创建远程仓库、首次推送、可选打tag。支持git filter-repo历史敏感信息重写、force push、tag管理。Windows下走Credential Manager自动认证。触发词:"发布到GitHub"、"上传到github"、"
可迭代相关的挑战
可迭代
/**
* @param {any} data
* @return {object}
*/
function createCustomIterable(data) {
return {
[Symbol.iterator]() {
let index = 0;
return {
next() {
if (index < data.length) {
return {
value: data[index++],
done: false,
};
} else {
return {
value: undefined,
done: true,
};
}
},
};
},
};
}
// Usage example:
const customIterable = createCustomIterable([1, 2, 3, 4]);
// Using for...of loop
for (const item of customIterable) {
console.log(item);
}
/**
* 1
* 2
* 3
* 4
*/
// Using spread operator
const arrayFromIterable = [...customIterable];
console.log(arrayFromIterable); // => [1, 2, 3, 4]
// Using Array.from()
const anotherArray = Array.from(customIterable);
console.log(anotherArray); // => [1, 2, 3, 4]
参考
- 迭代器 - mdn
- 迭代协议 - mdn
- [iterator.prototypesymbol.iterator - mdn](https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/iterator/symbol.iterator)

















