4 ms·
JS is so easy to cock up in, I commonly find myself logging to the console just to make myself sure of the scope and other things. This technique totally went
by FuzzyDunlop 15y ago
JS is so easy to cock up in, I commonly find myself logging to the console just to make myself sure of the scope and other things.
This technique totally went to shit when I came across one of our scripts that gratuitously used `apply()` all over the place.
The other common error is array iteration, and I've not quite understood why iterating through one array in the same scope as where it was created works fine, but passing it to another function and performing the exact same routine also goes through the prototype methods after the elements.
Of course, particularly with the var mistake, you'd never really understand the magnitude of it until you attempted to use JS on the server side. This post has, quite thankfully, likely exposed a bug in my own code I couldn't quite understand a while ago. :)
- ruethewhirled 15y agoif you want to filter out prototypes you can use object.hasOwnProperty( property )
- bzbarsky 15y ago> also goes through the prototype methods after the > elements. That shouldn't be happening. If you see it happening, your JS implementation is just buggy. In fact, this testcase: <script> window.onload = function() { var arr = window[0].arr; for (var i in arr) { alert(i); } } </script> <iframe src="data:text/html,<script>arr=[1];</script>"> </iframe> alerts only "0" in WebKit+JSC, Gecko, Presto. Chrome has some sort of bizarre security policy here that keeps the script from working, so no idea what WebKit+V8 does.