6

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

1

4 Answers 4

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(){}
Sign up to request clarification or add additional context in comments.

2 Comments

No "pretty much" about it. They are functions, and objects.
@Chad: No +1 for that. +1 would be to include a link explaining what "first class" means, 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..")
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.

20 Comments

-1 expression statements (which the assignment is) should be terminated with a semi-colon.
@Š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)?
Also, you cannot assign properties to variables that contain primitive values.
@Šime Vidas: I'm not VoteyDisciple.
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 =)
|
3

In JavaScript all functions are objects.

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

Comments

-1

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.

1 Comment

Always include relevant information from the cited article in your answer because, as in this case, web pages/sites can disappear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.