I am trying to get my head around how Javascript function behaves. Is it a function or an object or both?
+2
A:
In JavaScript all functions are objects.
Functions are objects that can be called. (They have a internal [[Call]] property)
Šime Vidas
2010-10-15 16:23:12
+6
A:
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
2010-10-15 16:23:33
+1 for mentioning "first-class"
Chad
2010-10-15 16:26:55
No "pretty much" about it. They are functions, and objects.
T.J. Crowder
2010-10-15 16:27:03
@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
2010-10-15 16:52:07
@palswim - but I have an example right below, demonstrating that...
meder
2010-10-15 16:56:13
-1 expression statements (which the assignment is) should be terminated with a semi-colon.
Šime Vidas
2010-10-15 16:26:52
@Š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
2010-10-15 16:27:58
Also, you cannot assign properties to variables that contain primitive values.
Šime Vidas
2010-10-15 16:28:15
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
2010-10-15 16:28:36
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
2010-10-15 16:31:49
@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
2010-10-15 16:33:29
@Š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
2010-10-15 16:33:38
@Š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
2010-10-15 16:33:39
@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
2010-10-15 16:34:32
@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
2010-10-15 16:39:22
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
2010-10-15 16:42:03
@Crowder Well, I guess -1 for typos is a bit to harsh. I'll try to just comment next time. :)
Šime Vidas
2010-10-15 17:00:15
@Š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
2010-10-15 17:03:25
@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
2010-10-15 17:04:52
@VoteyDisciple: Oh, I just had a look at your original answer and I see what you mean.
casablanca
2010-10-15 17:07:15
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
2010-10-15 17:40:00
@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
2010-10-15 17:51:14
A:
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.
Santiago Lezica
2010-10-15 16:25:49