Hacker News new | past | comments | ask | show | jobs | submit login

Your comment touches a lot on how to get an idea of the overall architecture, and I thank you for it.

My main problem though is that I can't seem to see the purpose of functions, because more often than not they are written in a, to me, foreign way.

An example I have is this uncommented if statement:

`if (tab.url.toLowerCase().indexOf("https://facebook.com") > -1)`

Should it be clear to me immediately that this means "If the target website is open"? It took me a solid minute just to understand this statement.

Are there some common design patterns I should memorise?




>> * Practice: The more you read and write code, the more proficient you'll become, the more intuition you will develop and thus the faster you'll be when skimming over unknown code.

> if (tab.url.toLowerCase().indexOf("https://facebook.com") > -1)

> Should it be clear to me immediately that this means "If the target website is open"? It took me a solid minute just to understand this statement.

In theory you should figure out what the url-property of tab is (e.g. by logging it's value to the console). Then you see it is a string and check the javascript reference for toLowerCase() and indexOf().

When you've gained some experience you'll recognize those two functions immediately and know what they do and just assume that tab.url is a string (and you'll be mad at the original developer if it isn't).


It's just vanilla Javascript, not some "foreign way". Checking `"somestring".indexOf(something) != -1` is common idiom in JavaScript because function indexOf returns -1 when there is no match. Just look for the definition of indexOf method on MDN: https://developer.mozilla.org/pl/docs/Web/JavaScript/Referen...

Maybe it's weird but what else could indexOf return? It couldn't return 0, because strings in JS starts from 0 (not from 1), it couldn't return null, because JS is weak typed so in some circumstances null would be not different than 0.

I'm glad that JS is not PHP (when some functions can return number or boolean or something else "just because") ;)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: