Learning to loathe Javascript
Nov. 9th, 2011 09:27 pmI'm adding the templating function to my link poster, so that different people can have different looking posts.
And I figure that if I'm doing that then I should have a "reset to default" button, to set it back to an unfucked state.
Should be simple enough - all I need is a button that you click, and a function that sets some text in the textarea holding the template. How hard can it be?
The first thing that got me was that my button was called "ResetTemplate" and my function was called "ResetTemplate" and when I called "ResetTemplate()" from the onClick of the button, guess what happened?
Did you guess "Nothing at all"? Because that's exactly what happened. Which left me completely baffled for about 45 minutes until I created a "test" function, which worked, and then slowly tweaked it until it contained exactly the same text as the ResetTemplate function, at which point the only possible explanation was the name of the function. Which wasn't my idea of fun.
And then, having got that far, I reasoned that as I had a document, containing a form, containing a fieldset, containing a textarea, I should have something like:
document.DeliciousPoster.TemplateFields.PostTemplate.value = "Replacement Text";
But no, that doesn't work either. Because, it transpires, the PostTemplate textarea isn't part of the TemplateFields fieldset. It's directly part of the form. Which makes no sense to me at all, but there you go, apparently that's how it works.
So if anyone would like to point me in the direction of a good book, or tutorial, or somesuch, that would help me avoid constantly falling into these pitfalls, then I would appreciate it.
(I'm also constantly frustrated by the lack of autocomplete. Having to actually remember the names of functions/methods/properties feels hopelessly old-fashioned. But I don't think there's much I can do about that...)
And I figure that if I'm doing that then I should have a "reset to default" button, to set it back to an unfucked state.
Should be simple enough - all I need is a button that you click, and a function that sets some text in the textarea holding the template. How hard can it be?
The first thing that got me was that my button was called "ResetTemplate" and my function was called "ResetTemplate" and when I called "ResetTemplate()" from the onClick of the button, guess what happened?
Did you guess "Nothing at all"? Because that's exactly what happened. Which left me completely baffled for about 45 minutes until I created a "test" function, which worked, and then slowly tweaked it until it contained exactly the same text as the ResetTemplate function, at which point the only possible explanation was the name of the function. Which wasn't my idea of fun.
And then, having got that far, I reasoned that as I had a document, containing a form, containing a fieldset, containing a textarea, I should have something like:
document.DeliciousPoster.TemplateFields.PostTemplate.value = "Replacement Text";
But no, that doesn't work either. Because, it transpires, the PostTemplate textarea isn't part of the TemplateFields fieldset. It's directly part of the form. Which makes no sense to me at all, but there you go, apparently that's how it works.
So if anyone would like to point me in the direction of a good book, or tutorial, or somesuch, that would help me avoid constantly falling into these pitfalls, then I would appreciate it.
(I'm also constantly frustrated by the lack of autocomplete. Having to actually remember the names of functions/methods/properties feels hopelessly old-fashioned. But I don't think there's much I can do about that...)
no subject
Date: 2011-11-09 10:30 pm (UTC)no subject
Date: 2011-11-09 10:32 pm (UTC)What was I doing wrong?
no subject
Date: 2011-11-10 01:52 am (UTC)1) If you're writing raw Javascript by hand, stop now!. Go and have a look at e.g. JQuery, and if that looks like what you want, download it or pull it in from e.g. Google's cache.
2) WebKit browsers (e.g. Chrome or Safari) have a damn useful inspector pane that does do auto-complete. Switch to the console and explore object hierarchies and properties interactively like a dynamically-typed language; anything you dump to the console (by calling e.g. console.log(object)) will get dumped in its full form, and you can explore it via disclosure arrows and so on. This only works if you dump only the object, e.g. console.log(object) rather than console.log("This random object I have is " + object)
Then get used to Javascript stupidities, like using + for a concatenation operator, or the really silly one where parseInt("12345") == 12345 but parseInt('012345') == 5349 because the leading 0 means octal, obviously. (You should have been specific and said parseint('012345', 10) because people decide to use bases other than 10 all the time in real life.)
no subject
Date: 2011-11-10 01:55 am (UTC)no subject
Date: 2011-11-10 01:58 am (UTC)no subject
Date: 2011-11-10 05:39 am (UTC)no subject
Date: 2011-11-10 08:13 am (UTC)no subject
Date: 2011-11-10 08:12 am (UTC)2) Chrome also doesn't show up "document.DeliciousPoster" in its autocomplete. Any ideas why?
no subject
Date: 2011-11-10 08:49 am (UTC)2) Pass; possibly it's only auto-completing methods? I get plenty of auto-complete on document.DeliciousPoster itself.
no subject
Date: 2011-11-10 07:10 am (UTC)no subject
Date: 2011-11-10 08:13 am (UTC)no subject
Date: 2011-11-10 08:44 am (UTC)no subject
Date: 2011-11-10 08:58 am (UTC)no subject
Date: 2011-11-10 09:10 am (UTC)no subject
Date: 2011-11-10 11:26 am (UTC)no subject
Date: 2011-11-10 11:25 am (UTC)no subject
Date: 2011-11-10 11:29 am (UTC)no subject
Date: 2011-11-10 11:31 am (UTC)(Do you understand machine code though?)
no subject
Date: 2011-11-10 11:34 am (UTC)(And I've generated code in IL, which is the .Net version of assembly)
no subject
Date: 2011-11-10 01:34 pm (UTC)I don't have any personal experience with javascript or jquery: I used javascript when I was a teenager for some hobby projects that I never finished (old link: http://www.vickeridge.clara.net/main.htm). But the second-hand opinion I get from seeing answers on stack overflow seems sufficiently widespread and reasonable that I'm pretty sure it's right even without any domain knowledge, and is: just use jquery. Always. (eg. http://stackoverflow.com/questions/471597/is-jquery-always-the-answer).
I think (although I don't have personal experience) an appropriate analogy might be the c standard library. Yes, you should be able to reimplement it all from scratch, and some of the functions a beginner maybe should write themself the first time -- but others I think it's much better to take as a black box and learn about the messy details (down to and including the exact architecture of a processor) later, often years later. I guess (although I don't know) understanding how jqueary implements things in javascript is useful to know, and following along with what's going on under the scenes is useful, but the impression I get is that javascript+jquery is the language of choice to start learning, and learning javascript alone is like learning C without functions: possible, and possibly informative, but generally not actually helpful...
no subject
Date: 2011-11-10 01:41 pm (UTC)no subject
Date: 2011-11-10 01:46 pm (UTC)no subject
Date: 2011-11-10 01:48 pm (UTC)no subject
Date: 2011-11-10 02:03 pm (UTC)no subject
Date: 2011-11-10 02:08 pm (UTC)