5 ms·
Hidden Features of JavaScript
- river_styx 18y agoWow, the top answer really doesn't understand first class functions at all.
- bradgessler 18y agoYeah, that's a pretty bad example!
- ericwaller 18y agoThat may not be a particularly interesting example, but I don't think it's bad. var a = 5; var a = function() { alert('hello world'); }; var a = 2; It's not a closure (which is what I assume you were hoping for), but it definitely reveals something pretty unexpected about the language for someone who has no experience with first class functions.
- bradgessler 18y agoYeah, that's also pretty bad. It doesn't show what you can do with the function.
- johns 18y agoThen go post a comment on that answer or post a better answer. If you prove his answer wrong, it can be edited. It's a wiki as much as it is a Q&A site.
- vegashacker 18y agoI almost was inspired to, but it said something like comments require 50 points. I also don't know what I'd write. As a programmer, I feel crippled without first-class functions, but I'm not sure I know how to distill them into a few choice lines. My first instinct was to use something like map, but I don't think JS has such functions built-in.
- AndyKelley 18y ago"answers" don't require 50 points. In fact I don't think answers even require login.
- davidw 18y agoI found the whole "you can't do this, or that, or this other thing without points" kind of annoying.
- vegashacker 18y agoI too found it annoying, but it still might be a good idea. Maintaining quality of content is a big deal. Obviously you'll lose some good posts, but in the end it might be worth it.
- technoguyrob 18y agoI don't know why the second-top one has so many votes, it's completely wrong. var x = 1; var y = 3; var list = [0,1,2]; x in list; //true y in list; //false 1 in list; //true y in [3,4,5]; //true That last expression is false. This is because the "in" operator does not check for values in the list; it checks for keys. Try this: javascript:alert(3 in [3,4,5]) The only things that will be true for "X in [3,4,5]" are 0, 1, and 2 for X (since these are the natural keys for the terms in the array). More specifically, "in" in general checks for members associated with objects (e.g., keys in an array, methods of a class, etc.): javascript:alert("The setFlag method " + ("setFlag" in {setFlag:function(){}} ? "exists" : "does not exist")) EDIT: This makes the keyword considerably less useful (since indexOf doesn't work for the Array object in IE--although jQuery has a way around this), although it can be used to replace the less elegant if (typeof object.method == "undefined")
- wayne 18y agoOther fun 'in' trivia: It looks through the object's entire prototype chain: http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
- johns 18y agoGo post a comment on that answer to clarify it.
- PieSquared 18y agoOh whoa! I never realized I could just type "javascript:..." into the status bar in Firefox and have it run it. That's awesome. (In retrospect, I realize I should've realized it, because I've always known that the javascript links are "javascript:functionCall()" or some such.)
- axod 18y agoThis is really scary. Hidden features?? Was this posted to show just how much misinformation and dumbing down there is at stackoverflow? As if we need more misinformation about javascript on the net.
- jrockway 18y agoYou know, it's kind of sad. Before reading the article, I was going to post a reply to your comment like "but omg, javascript has functions that are values!!1". Then I read the article, and found that that's the first "hidden feature". I guess "hidden feature" means "something that's not in PHP". I also like how people use terms like "null coalescing operator" when everyone else calls it "short circuiting". They are so smart!!11!
- jaf656s 18y agoshort circuiting is only half of why this example works. The other half is that boolean operators in javascript don't implicitly cast the result as a boolean type.
- deleted 18y ago[deleted]
- anthonyb 18y agoThe "Hidden features of PHP" section is pretty damn funny actually, but not in a good way. I can summarise it for you if you're lazy: "ZOMG - PHP has arrays! Arrays are like, super cool!"
- johns 18y agoYou know what's great about StackOverflow though...you can go fix it if you want. Go post a better answer, get upvoted, misinformation is minimized.
- axod 18y agoYou assume the people there would be able to recognize a "better answer". Just saying...
- wayne 18y agoIf you want to learn something new about JavaScript, just skim through Douglas Crockford's articles. As long as I've been doing JavaScript, I learn something new or rediscover something every single time: http://javascript.crockford.com/ http://javascript.crockford.com/
- kylec 18y agoI can understand covering the site itself, but posting links to the content? Did we run out of real Hacker News?
- iamelgringo 18y agoYou could always try to find interesting content and post it: http://news.ycombinator.com/submitted?id=kylec http://news.ycombinator.com/submitted?id=kylec
- johns 18y agoI have enough rep on the site to edit any question or answer. If you find something that's factually incorrect, post a comment on it, link it here and I'll edit it. Get off your high horses and help some people out.
- gruseom 18y agoHigh horse? I feel no impetus to participate in that community. What I do find interesting is how it seems very much an extrapolation of Jeff Atwood's blog - much as HN, I suppose, is largely populated by people who are attracted to Paul Graham's essays. On the one hand, this is perfectly natural; on the other hand I find it fascinating. Edit: I'm not saying that the people in these communities necessarily agree with the opinions of their founders.
- andreyf 18y agoThis is a matter of taste, of course, as we don't have a good study of programming code comprehension yet, but I think the coolest part of JS are cond statements using the trinary operator: var foo = predicate ? "one" : predicate2 ? "two" : "default"; And short-circuit and/or: var foo = uncheckedFoo || "default"; var wiz = (bar && bar(foo)) || "default"; Truthy values get in the way sometimes (especially 0), but it sure as hell beats a ton of if...else crud. Worst part of JS, while we're at it: true == "0" == 0 == false
- andreyf 18y agoHere's another neat example of short-circuit or: function getFoo(f) { if (f) { if (foos[f]) { return foos[f]; } else { return f; } } else { return "error"; } } Is (almost) the same as... function getFoo(f) { return foos[f] || f || "error"; } The difference in behavior being if foos[undefined] is set...
- boucher 18y agotrue == "0" == 0 == false This is untrue. > true == "0" false > "0" == 0 true > 0 == false true > true == "0" == 0 == false false
- DougBTX 18y agoI think he meant something like this: true == !0 == "0" == false Which still isn't that interesting, since it is much the same as: true == (false == false) More interesting, something like: > ! 0 == "0" false > 0 == ! "0" true Since "!" acts as if false === 0.