What I've been doing...
Jun. 28th, 2013 10:45 pmWell, not "tomorrow", (as suggested as a possibility in my last post), but tonight instead. Things just seem to crop up and I've never enough time.
Anyway, one of the things I've been doing of late is working on a website, (well, page, or eventually pages of one), with a mate who's in Scotland. We'd been trying to think of something to do on the web for quite a while, (read 'a few years'), and it seems it's very hard to come up with an idea that interests someone else. The result being we're doing this thing, just to be doing something with a VPS server we share. And it'll hopefully see the light of day soon, though don't get too excited as it's kinda specialised and won't be of real use to most people.
I've found it fun, anyway. I'm mostly doing the website, which means I'm using JavaScript for the first time. (It's more app than webpage.) JavaScript hasn't proved much of a problem, though having got tired of repeating "for (var i = 0; i < whatever.length; i++) {", I looked around for a forEach function, and found it wanting. It does have a forEach method for arrays, but it only works with arrays. Most odd.
So I've come up with my own forEach function, which works with arrays, objects and strings...
This seems to work well and is a lot cleaner than your bog standard for loop, though let me know if there's any possible issues with it - or there's a better way.
Ideally you'd be able to write...
I don't know if it's possible to create such a function in JavaScript though.
And now to catch up on people's journals. Starting at 12:35am. Sigh...
Anyway, one of the things I've been doing of late is working on a website, (well, page, or eventually pages of one), with a mate who's in Scotland. We'd been trying to think of something to do on the web for quite a while, (read 'a few years'), and it seems it's very hard to come up with an idea that interests someone else. The result being we're doing this thing, just to be doing something with a VPS server we share. And it'll hopefully see the light of day soon, though don't get too excited as it's kinda specialised and won't be of real use to most people.
I've found it fun, anyway. I'm mostly doing the website, which means I'm using JavaScript for the first time. (It's more app than webpage.) JavaScript hasn't proved much of a problem, though having got tired of repeating "for (var i = 0; i < whatever.length; i++) {", I looked around for a forEach function, and found it wanting. It does have a forEach method for arrays, but it only works with arrays. Most odd.
So I've come up with my own forEach function, which works with arrays, objects and strings...
<body>
<div>a</div>
<div>bb</div>
<div>ccc</div>
<p id="output">output</p>
<script>
function forEach(list, func) {
// Iterate through an array, object or string.
// Usage: forEach(arr-obj-or-str, function(i) { ... });
for (var i = 0; i < list.length; i++) {
func(i);
}
}
var result = "";
var divs = document.getElementsByTagName('div');
forEach(divs, function(i) {
result = result + " " + divs[i].innerHTML;
});
var arr = [" AB", " CD", " EF"];
forEach(arr, function(i) {
result = result + arr[i];
});
var str = "xyz";
forEach(str, function(i) {
result = result + " " + str[i];
});
document.getElementById('output').innerHTML = "result: " + result;
</script>
</body>
This seems to work well and is a lot cleaner than your bog standard for loop, though let me know if there's any possible issues with it - or there's a better way.
Ideally you'd be able to write...
forEach(arr-obj-or-str, i) {
...
}
I don't know if it's possible to create such a function in JavaScript though.
And now to catch up on people's journals. Starting at 12:35am. Sigh...
(no subject)
Date: 2013-06-28 04:21 pm (UTC)(no subject)
Date: 2013-06-29 01:38 am (UTC)Anyway, good to know my approach is a reasonable one.
(no subject)
Date: 2013-06-29 02:21 am (UTC)(no subject)
Date: 2013-06-29 05:20 am (UTC)(no subject)
Date: 2013-06-30 07:37 am (UTC)http://addyosmani.com/resources/essentialjsdesignpatterns/book/
would seem to provide some good reasons for such libraries. See the section "We already use patterns everyday" near the start.