Friday, September 5, 2008

Variable scope in Javascript

I'm from an old skool of JavaScript where it was a nice add-on and everything was done on the server. Sure, i've used all the JQuery, Ajaxy, Prototypey frameworks out there but i never went back to look at the core of the language - well at the moment i'm working on something and writing a lot of script.

So i learned something new. A lot of frameworks were using the following syntax in their libraries..

(function() {
...

})();


It's fair to say i was going mad trying to figure out why. I mean you put an alert in there and it pops up when the script loads. So why bother with the function? Why not just write your code as we used to - directly in the page?

Well, thanks to this paper, I have discovered why and this note is as much for my future reading as yours :)

Turns out that you can locally scope variables within these anonymous javascript functions which more importantly means you don't screw up global vars that are being used elsewhere. So you can confidently write the following ...
var person = 'steven';
(function() {

var person = 'xavier';

alert("Person 1 " + person);

})();

alert("Person 2 " + person);



... and you will get "Person 1 xavier" and "Person 2 steven" - notably the global variable "person" was redeclared within the scope of the anonymous function and the name changed, but as it was declared with "var" it does not overwrite the global value. If you did NOT use "var" in the function, you would change the value globally. So it works like every other programming language - just a wee bit different in term of syntax :)

Wednesday, September 3, 2008

V8, Chrome and the DOM

V8 is the Javascript engine for Chrome, the new Google web browser. It is very fast.

V8 does not provide DOM or XML DOM support. This was confirmed to me by a member of the team at the V8 google group.

This leaves me with a question i hope someone can answer.

Most of the scripts i write either use the DOM (getElementById) or the Xml Dom (childNodes[0].nodeValue) and so on. That is a LARGE part of my scripts.

So if the engine does not provide support for this, what happens to my scripts? Do they go really fast until they need to query the DOM and then slow down? The net effect in my case could be a lot of slowing down. I my experience, querying the DOM is the slowest part of your scripts (unless you write non-typical).

Haven't had time (and won't today) to run tests so if you know the answer please let me know.

Tuesday, September 2, 2008

"Google Suggests ..." - don't use Chrome :)

I had to put this one up. I was in Chrome and tried to change my pic in the Groups and here's what i got (changeing pic works fine in friendfeed, for example).


my first chrome weird thing

This may just chance, but the only site i can't get to is webkit.org - which Chrome runs on top of :)

Not only that, it thinks it is "null" (other sites that can't actually be found give a DNS error).

Anyone else get to http://webkit.org ?


Chrome Auto-complete Ideas

Whilst some of what i have seen from Chrome is what i would expect of a browser of this generation (much of it is in IE 8 too), there are some things such as the V8 Javascript compiler which looks to be very cool. I am only halfway through the book, but wanted to write a post on auto-complete.

Google say Chrome will mean you don't need to bookmark pages any more and will provide an improved auto-complete. Well, here's my thoughts...

"Auto-complete" should be much more expansive. I rarely remembers URL's and even things i have found and bookmarked can be a pain to find again. In fact i tend to bookmark things and never look at them again. So i may be searching for something i have already found. Auto-complete needs to add techniques to discover information you want to keep track of. Tags in a delicious style are an obvious way. Add to that some kind of personal strength and it may be very useful. So i type in "Javascript Cross Domain problem" and get a view of a set of links i previously found useful - coz let's face it, you'll end up going to google, and in the process of a single search modify the query, view certain pages, like some discard others and repeat. In that *session* I should be able to get back to all the pages i found useful. I don't mean caching the pages, i mean the semantic link to those pages.

Can you imaging a little package of links you found useful related to a search concept that you can reuse in the future. I would LOVE this. So where Chrome shows a new tab with the top links it it, the auto-complete concept would extend to the search package i discussed above.

ps. stuff such as gears, prototype and so on should be part of the environment itself... maybe that is later in the comic :)

pps. Is it true that the Joker appears in chapter 5??