2

ok, I was experimenting (getting straight in my head) all this object oriented like stuff javascript can do, and Im simulating inheritance with functions, adding functions to functions (too cool!) and I had a AHA! moment.

var myArray = [function(){console.log("im in an array!");}, 2, "fly feet!"];
myArray[0]();

Of course, now that Ive done it, Ill find its a common thing and useful for somesuch thing... BUT I DISCOVERED IT!!!

Anyone care to share their AHA! moments?

jason
  • 4,721
  • 8
  • 37
  • 45
  • 4
    Good job, now make this a Community Wiki :) – Anurag Jul 20 '10 at 20:42
  • possible duplicate of [Hidden Features of JavaScript?](http://stackoverflow.com/questions/61088/hidden-features-of-javascript) – Robusto Jul 20 '10 at 20:54
  • Not a javascript guy, but I am an OO developer. Had to punch this into an HTML file and test it. Nothing! What was I supposed to notice? –  Jul 20 '10 at 21:25
  • 1
    console.log is a firebug object / function, so if youre not using firefox with that addon, I believe it wont work. In that case, the fix would be replace console.log(...) with document.write(...) or just alert(...). The thing it does thats cool is stuffing a function into an array, and then calling it with array notation. For me, its starting to blur the line between functions, arrays, and other objects. – jason Jul 21 '10 at 00:41
  • Ok, I got alert to work. So, basically, the array values 2 and "fly feet" do nothing - as I suspected, but I wasn't certain. –  Jul 22 '10 at 18:38
  • Check out http://stackoverflow.com/questions/61088/hidden-features-of-javascript – Mike Tunnicliffe Jul 20 '10 at 20:43

3 Answers3

2

When I understood the answer to this question:

How exactly does the JavaScript expression [1 [{}]] parse?

Your AHA moment is an example of first-class functions.

Community
  • 1
  • 1
Jesse Dhillon
  • 7,841
  • 1
  • 34
  • 34
  • I realized thats what it was, that was the specific thing I was (have been) studying. I'd never seen it done in an array, had to try it, was all happy it worked! – jason Jul 20 '10 at 20:56
1

I don't think I had an AHA moment. After learning that everything most things in JavaScript are objects, I realized that something like this is possible:

console.log("I'm not wearing pants".replace('not ', ''))
// Produces: "I'm wearing pants"

As is this:

foo = function(operation) {
  operation();
}

pants = function() {
  console.log("I'm not wearing pants!!");
}

foo(pants); // Produces console output of "I'm not wearing pants!!"
Hooray Im Helping
  • 5,194
  • 4
  • 30
  • 43
  • Not *everything* in JavaScript is an object, there are *primitive values* such string, number, boolean, undefined, and null *values*. There are also primitive value wrapper objects, for string, number and boolean values, e.g.: `typeof "" == "string"; // primitive` vs `typeof new String("") == "object"; // primitive wrapper` – Christian C. Salvadó Jul 20 '10 at 21:05
  • whoa... very cool. That seems very close to how eval works. – jason Jul 21 '10 at 00:45
1

Since functions are first-class objects in JavaScript, you can use them anywhere you would use an object, that includes storing them in an array or even doing things like returning a function from another function.

function one() {
    alert("one");
    function two() {
        alert("two");
    }
    return two;
}

one()();
flpmor
  • 861
  • 5
  • 10
  • Whats happening with the ()(); ? edit NM I figured it out. One() evaluates to two, which gets the other () slapped on it. Badass. – jason Jul 21 '10 at 00:50