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 ?
v1is not a property of anything. It is a variable.foo.v1and a variable namedv1infooare unrelated. You can assign tofoo.v1if you like, or passfooto other functions, etc. like any other object, but variables aren’t reflected in functions’ properties in any way.v1in the scope of function foo