for-in
loops should include an if
statement JS-0051 84 if(!(watched[board] instanceof Array))
85 watched[board] = [];
86
87 for(const t in watched[board]) { 88 let thread = watched[board][t]; 89 if(typeof thread === "number") { 90 thread = watched[board][t] = {id: thread}; 91 } 92 if(thread.id === threadID) return; // thread is already in the watched list 93 } 94 getThreadJSON(threadID, board).then(data => {
95 const op = data.posts[0];
96 const threadObj: WatchedThreadJSON = {
20 continue;
21 }
22 $(document).trigger("beginNewPostsCheck");
23 for(const t in watched[board]) { 24 const thread = watched[board][t]; 25 if(thread.err !== undefined) continue; 26 getThreadJSON(thread.id, board).then(data => { 27 if(data.posts.length > thread.posts) { 28 // watched thread has new posts, trigger a menu update 29 if(currentPage.board === board && currentPage.id === thread.id) { 30 // we're currently in the thread, update the cookie 31 watched[board][t].posts = data.posts.length; 32 watched[board][t].latest = data.posts[data.posts.length - 1].no; 33 setStorageVal("watched", watched, true); 34 } 35 $(document).trigger("watcherNewPosts", { 36 newPosts: data.posts.slice(thread.posts), 37 newNumPosts: data.posts.length, 38 op: thread.id, 39 board: board 40 }); 41 } 42 }).catch(e => { 43 if(e.status === 404) { 44 watched[board][t].err = e.statusText; 45 setStorageVal("watched", watched, true); 46 } 47 }); 48 } 49 }
50}
51
Looping over objects with a for in
loop will include properties that are inherited through the prototype chain.
This behavior can lead to unexpected keys in your for loop.
for (const key in foo) {
doSomething(key);
}
for (const key in foo) {
if (Object.prototype.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
for (const key of Object.keys(foo)) {
doSomething(key);
}