birguslatro: Birgus Latro III icon (Default)
[personal profile] birguslatro
Well, 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...

<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)
foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
From: [personal profile] foxfirefey
No, I think passing in functions in the way to go--that's how jQuery's generic each works, so it's a pattern that many will be used to.

(no subject)

Date: 2013-06-29 02:21 am (UTC)
foxfirefey: Fox stealing an egg. (mischief)
From: [personal profile] foxfirefey
The $ is just a shorthand for the jQuery function name; you don't have to use it, you could use jQuery() instead. Right now http://zeptojs.com/ is gaining ground as a jQuery-like alternative that focuses more on being small than backwards compatibility.

Profile

birguslatro: Birgus Latro III icon (Default)
Carl B. Latro

January 2023

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags