6

I am trying to get my head around how Javascript function behaves. Is it a function or an object or both?

rkg
  • 5,559
  • 8
  • 37
  • 50
  • possible duplicate of [Is Function really an Object](http://stackoverflow.com/questions/3941729/is-function-really-an-object) – Andy E Oct 15 '10 at 16:42

4 Answers4

8

Functions in javascript are first-class objects. So they're both functions and objects.

Because they are first class objects, you can assign a variable to a function and give it properties, eg:

var addName=function(){}; 
addName.blah = 1;

If they weren't first-class objects you'd be limited to this syntax but you can do it both ways:

function addName(){}
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • No "pretty much" about it. They are functions, and objects. – T.J. Crowder Oct 15 '10 at 16:27
  • @Chad: No +1 for that. +1 would be to include a [link explaining what "first class" means](http://en.wikipedia.org/wiki/First-class_function), since people like to throw that term around all over the place here. Or better yet, say that functions are first-class objects without using the term "first-class" (i.e. explain it in a way that normal people would understand: "Yes, Javascript lets you assign functions to variables and call the variables..") – palswim Oct 15 '10 at 16:52
4

It is both.

Everything is "data" in Javascript, including functions. I find this a good way to picture it:

var f = function() { alert('foo'); };

This is an assignment to a variable that's no different than if you'd written, say:

var f = new String('foo');

Either way, you can then write statements like f.bar = 'baz'; to assign properties to your object. The only difference is that the () operator (if you will) works only if the variable you have happens to be a function. f() makes sense if it's a function; f() makes no sense if it's a string or some other piece of data.

VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • -1 expression statements (which the assignment is) should be terminated with a semi-colon. – Šime Vidas Oct 15 '10 at 16:26
  • 3
    @Šime Vidas: Dude, by all means post the comment, but isn't the -1 a bit harsh for something that A) Is off-topic, and B) Is allowed by the language (even though relying on it is a *really, really bad idea*)? – T.J. Crowder Oct 15 '10 at 16:27
  • 1
    Also, you cannot assign properties to variables that contain primitive values. – Šime Vidas Oct 15 '10 at 16:28
  • No worry, I will reverse the -1 as soon as you correct that :) If I just posted the comment, there would be a smaller chance that you would correct it, and I don't tolerate bad code. – Šime Vidas Oct 15 '10 at 16:28
  • 1
    @Šime Vidas: I'm not VoteyDisciple. – T.J. Crowder Oct 15 '10 at 16:29
  • Ouch. Fixed the typo. I also didn't claim one can assign properties to primitives; that's why I chose to use a string. – VoteyDisciple Oct 15 '10 at 16:31
  • Reversed. See, wasn't that bad. – Šime Vidas Oct 15 '10 at 16:32
  • @Votey You say: "Either way, you can then write statements like f.bar = 'baz';". Well, in the case of var f = "foo"; you cannot. – Šime Vidas Oct 15 '10 at 16:33
  • @Šime Vidas - No, you cannot attach properties to primitives, directly. But, the variable they're stored to still acts like the equivalent object -- `var a = 3; a.foo = 'bar'; var b = a.toString();` – Jonathan Lonowski Oct 15 '10 at 16:33
  • @Šime Vidas - Expression statements don't have to be terminated at all, like T.J Crowder said. It would be valid if ECMAScript didn't have automatic semicolon insertion. – meder omuraliev Oct 15 '10 at 16:33
  • @Votey: The string you used is a primitive, you'd have to use `var f = new String('foo')` to use a String object. (One of those dusty corners of JavaScript where you sometimes step on a rusty nail.) Example: http://jsbin.com/ilafe4 – T.J. Crowder Oct 15 '10 at 16:34
  • Whoops again. That's what I meant. – VoteyDisciple Oct 15 '10 at 16:37
  • @meder I didn't say they have to, I said they should be... JavaScript is not easy for begginers as it is. Let's just agree that semicolons should be used at all times, and that code without them is bad code. – Šime Vidas Oct 15 '10 at 16:39
  • I'll recommend any novice learn the rules of where they are and aren't allowed. After that it's just personal preference. inimino, a friend of mine wrote a good blog post about this.. http://inimino.org/~inimino/blog/javascript_semicolons – meder omuraliev Oct 15 '10 at 16:42
  • @Crowder Well, I guess -1 for typos is a bit to harsh. I'll try to just comment next time. :) – Šime Vidas Oct 15 '10 at 17:00
  • @Šime Vidas: It's perfectly legal to assign properties to primitive types. When you do that, a temporary object (for eg. a `Number` object for numbers) is created and the property is assigned to that object, however this object is usually lost immediately and so you can't access those properties. – casablanca Oct 15 '10 at 17:03
  • @casablanca While that's true, Šime Vidas was correct in that the way I had typed the example it was not at all demonstrating what I had intended it to. – VoteyDisciple Oct 15 '10 at 17:04
  • @VoteyDisciple: Oh, I just had a look at your original answer and I see what you mean. – casablanca Oct 15 '10 at 17:07
  • 1
    You can do `var f = "foo"; f.bar = 4;`. What happens is `f` in `f.bar` gets converted to a `String`, then its `bar` property is assigned, then the converted `String` value is lost since nothing has a reference to it. but you _can_ do it =) – Claudiu Oct 15 '10 at 17:40
  • @Claudiu +1 Yes, that's what happens. With the distinction that the f variable is not changed at all in the process. Only its value is taken in order to create a String object, and so on... – Šime Vidas Oct 15 '10 at 17:51
3

In JavaScript all functions are objects.

Functions are objects that can be called. (They have a internal [[Call]] property)

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
0

Well, I'm not going to say "JavaScript functions are objects of first class", since everyone already said that, but if you want more on functions, take a look at this short page:

http://jqfundamentals.com/book/ch02s09.html

By the way, if you're planning on learning JavaScript and JQuery, that's a free online book for you.

salezica
  • 74,081
  • 25
  • 105
  • 166