0

I am new to javascript so please understand if the question is a bit naive. I have heard that functions are also objects in javascript . So that means functions can also have properties like objects. So I tried this :

var foo=function(){
        var v1=1;
        console.log(foo.v1);
    };
    foo();

The output of this is undefined. I dont understand what is happening. So when I declare the variable v1 in the function foo ,according to the result v1 is not a property of the function-object foo.If it is not the former then what is it a property of ? Could some one explain to me what is happening ?

2
  • 1
    v1 is not a property of anything. It is a variable. foo.v1 and a variable named v1 in foo are unrelated. You can assign to foo.v1 if you like, or pass foo to other functions, etc. like any other object, but variables aren’t reflected in functions’ properties in any way. Commented Aug 1, 2015 at 8:59
  • foo.v1 is indeed undefined, all you have defined is a var v1 in the scope of function foo Commented Aug 1, 2015 at 9:00

2 Answers 2

1

You're right in Javascript function is a object. You can have attribute in a function object, examples are link length etc.

according to the result v1 is not a property of the function-object foo.

v1 is just a variable defined in function foo, it is not a attribute.

To add a attribute you could use foo.v1 = "1" to add attribute v1 to object foo.

If you use console.log(v1) instead of console.log(foo.v1). You'll see output 1. Here you're accessing a local variable inside a function.

You might think var foo is already a object why can't I access it inside the function? This because these two foo variables are in different scopes. You might want to learn more about function scope

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick answer. Could you please elaborate on the last sentence, I did not understand it properly
0

Object properties and variables are not the same thing. Functions can have both local variables (this includes function arguments) and properties:

function foo () {
    v1 = 1; // this is a local variable
    return [
        v1,    // local var above
        foo.v2 // object property
    ]
}

foo(); // returns [1,undefined]

foo.v2 = 2;
foo(); // returns [1,2]

So you see, functions are indeed objects. But variables inside the function have nothing to do about the fact that functions are objects.

Side note: the fact that functions are first-class objects and that functions can be defined anonymously (without a name) are two different features. In C for example, functions are also first-class objects - that is, you can assign them to function pointers. But in C you can't declare functions without a name.

function bar() {}

b = bar; // this demonstrates that functions are first-class objects
c = function(){}; // this demonstrates that functions can be anonymous

4 Comments

Ok so basically the difference between 'normal' objects in javascript and 'function- objects' in javascript is the fact that the latter can have local variables in it. On a side note where are the local variables actually defined, is it in the scope of the function ??
Yes, it's in the function scope. Though, scope in javascript isn't like it is in C - they can persist even after the function has returned. Look up info on closures.
See my answer to this question for a deep/detailed discussion of scopes and closures: stackoverflow.com/questions/26061856/…
Thank you very much . That really cleared the concepts for me

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.