Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've learned javascript at school but since I'm working with it and study about it every day, I've found very particular aspect of javascript that I didn't know about. Which at first, was very hard to understand for me and finally, I found it very usefull and easy to implement. And in the final, it gives to my code some kind of "beauty".

An example I've once seen:

function getter( input )
{
    result = {
        foo1 : 'bar1',
        foo2 : 'bar2',
        foo3 : 'bar3'
    }[input] || input || "default";

    return result;
}

Do you guys have other examples of particular use you make of Javascript ?

Thank you

PS: I use the term particular use because it might be unusual for any Javascript beginner. I believe this question is most likely to belong to the community wiki.

share|improve this question

migrated from stackoverflow.com Feb 3 '11 at 16:59

This question came from our site for professional and enthusiast programmers.

    
Since this doesn't have an objective answer, it should probably be a Community Wiki. –  user12157 Feb 2 '11 at 15:20
    
@patrick dw, I totally agree but I can't convert/create a community wiki post. –  Cybrix Feb 2 '11 at 15:22
    
I see. I didn't realize that more rep was needed to make a CW. There's no CW checkbox under the question's edit area? EDIT: Just looked at meta, and sure enough, only moderators can do it now. –  user12157 Feb 2 '11 at 15:25
    
@patrick dw, for some reason only 10 reputations is required to create wiki post. But the checkbox wasn't there and in fact, I've not seen it for a while. (When I created my account before I remember that checkbox). –  Cybrix Feb 2 '11 at 15:31
    
@Cybrix: Yeah, there are some posts on meta indicating that now only moderators can do it. –  user12157 Feb 2 '11 at 15:33
show 7 more comments

3 Answers 3

SELF INVOKING FUNCTIONS

The following example applies an “negative” class on every input element who’s numeric value is below 0.


(function (elements) {
    for (var i = 0; i < elements.length; i++) {
        if ((elements[i].value * 1) < 0) {
            elements[i].className = 'negative';
        }
    };
})(document.getElementsByTagName('input'));

In an effort to protect the global object, all JavaScript applications should be written within a self-invoking function. Although, this is a very basic concept, I found this real odd, unusual, cool and exciting at the same time.

share|improve this answer
    
Also one of the idiomatic ways of creating event handlers in a loop (to avoid the handlers closing over the same binding). –  Jesse Millikan Feb 3 '11 at 17:24
add comment

jQuery

jQuery makes such extensive use of closures and wrapping objects that it pretty much turns JavaScript into a completely different language.

Example:

$('ul.level-2').children().css('background-color', 'red');

From http://api.jquery.com/children/

jQuery is interesting in that often it's completely ambiguous whether you have a single element or a list of elements, and as a result most (all?) of the same functions can be run in either case.

share|improve this answer
5  
actually in jQuery you always have a list, it just may be a list of 1 element. –  Zachary K Feb 16 '11 at 13:46
add comment

I tend to do a lot of list operations and recursion. Almost treating javascript as if it was lisp.

somthing like this

   function getNegative (elements){ 
      return elements.filter(function (el){  return el< 0; });
   }

very clean and easy to understand

share|improve this answer
    
Yes indeed it's very clean. –  Cybrix Feb 16 '11 at 14:09
    
actually I will often stack array ops, put 3-5 of them in a row, each one is generally a function of 1-3 lines of code. –  Zachary K Feb 16 '11 at 14:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.