Hidden Features of JavaScript
stackoverflow.com
stackoverflow.com
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")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.
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!
"ZOMG - PHP has arrays! Arrays are like, super cool!"They just think up how they expect something should work, or what they think something should be called, and try it. Then come in and ask why it's not working. They often exchange misinformation with each other without either party realizing it's false.
Then if you suggest to them they read up on the API for what they're trying to do, they won't... they'll just keep trying random things, trying to get someone to feed them the answer.
I think javascript is particularly bad for this, even though there are some really good books and references out there now.
This is not meant to disparage people like web designers who come at things from an artistic angle, but a lot of people who write Javascript are doing it in a tertiary role -- in support of HTML in support of producing a visual design that they envision. That these people don't have a solid foundation in programming shouldn't be alarming or disconcerting.
Ten years ago Javascript had a reputation of being a crap/toy language, largely for this reason. It really wasn't until GMail, IMO, when people started saying "Look, a JS app that doesn't suck!" and things kind of went forward from there.
Either you're talking about something different, or I don't understand what's wrong with such an approach... Although maybe 'experimental' is a better title?
...correction: On rereading your post a few more times, I realize it's just a misconception of terminology.
Any expert website is an ad hoc, ugly, slow implementation of Usenet featuring inferior comments.
The good thing about Usenet is the barrier to entry. Stack Overflow has already shown that it's wayyyy too easy to get and account and start incorrectly answering questions.
Surely you weren't expecting "hidden" features in any literal sense?
Personally, I expected something more hidden than core features of the language.
Just saying...
So if your aim is to get upvoted, that's your target market. Better to sound right than to be right in that environment.
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.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.
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.
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 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... true == "0" == 0 == false
This is untrue. > true == "0"
false
> "0" == 0
true
> 0 == false
true
> true == "0" == 0 == false
false 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.