Do serious JavaScript coders use string-formatting methods like big()?
April 4, 2012 8:15 AM Subscribe
Do serious JavaScript coders use string-formatting methods like big()?
Best answer: Can't speak for most folks but I do a lot of fulltime Javascript dev and forgot those methods existed.
That being said the biggest reason to try not to use that style of formatting is that most of that these days will be handled in CSS.
Note it seems that what most of those methods do is wrap the string in the appropriate HTML tag and that kind of willy nilly addition to the DOM is kind of frowned upon.
posted by bitdamaged at 8:27 AM on April 4, 2012
That being said the biggest reason to try not to use that style of formatting is that most of that these days will be handled in CSS.
Note it seems that what most of those methods do is wrap the string in the appropriate HTML tag and that kind of willy nilly addition to the DOM is kind of frowned upon.
posted by bitdamaged at 8:27 AM on April 4, 2012
Best answer: I'm a serious javascript coder who had literally never heard of the big() function until just now. And now that I've heard of it I will never use it.
Wherever possible, you want to keep your javascript and your DOM separated, so that people who need to modify the layout in the future won't need to dig into the javascript code to do so.
Many js coders will inject a bunch of formatted html into the DOM (usually using string concatenation, e.g. foo.innerHTML = ''+variable+'' rather than the equivalent big() function, but same idea.) This is a Bad Idea, because it means your HTML layout is now partially constructed inside the javascript, which makes maintenance and debugging more difficult.
Best practice when you need to inject javascript values into a page like this is to use a template system -- something like mustache.js is often overkill, but you can accomplish the same thing by placing your 'template' in the DOM as a hidden node, and having the javascript copy that node, inject its variables as needed, and dump the result back into the page. That way if someone needs to change the layout they can modify that hidden node rather than having to edit the js function.
posted by ook at 8:53 AM on April 4, 2012 [3 favorites]
Wherever possible, you want to keep your javascript and your DOM separated, so that people who need to modify the layout in the future won't need to dig into the javascript code to do so.
Many js coders will inject a bunch of formatted html into the DOM (usually using string concatenation, e.g. foo.innerHTML = ''+variable+'' rather than the equivalent big() function, but same idea.) This is a Bad Idea, because it means your HTML layout is now partially constructed inside the javascript, which makes maintenance and debugging more difficult.
Best practice when you need to inject javascript values into a page like this is to use a template system -- something like mustache.js is often overkill, but you can accomplish the same thing by placing your 'template' in the DOM as a hidden node, and having the javascript copy that node, inject its variables as needed, and dump the result back into the page. That way if someone needs to change the layout they can modify that hidden node rather than having to edit the js function.
posted by ook at 8:53 AM on April 4, 2012 [3 favorites]
Best answer: Preview fail. That should read
foo.innerHTML = '<big>'+variable+'</big>'
posted by ook at 8:55 AM on April 4, 2012
foo.innerHTML = '<big>'+variable+'</big>'
posted by ook at 8:55 AM on April 4, 2012
(Thanks for the tip on avoiding messy HTML injection. That's really interesting.)
posted by steinsaltz at 10:17 AM on April 4, 2012
posted by steinsaltz at 10:17 AM on April 4, 2012
« Older I Don't Need a Law to Be Safe! | Preparation tips for a meeting with city officials... Newer »
This thread is closed to new comments.
posted by le morte de bea arthur at 8:26 AM on April 4, 2012