Updated:
This is an extended answer requested by OP in comment.
So OP was asking what if you have 2 functions within it. How would you call it? Easy, just return an object and assign the function handlers to a property each.
Example:
function Counter()
{
var count=0;
var counterIncrement = function()
{
count++;
console.log(count);
}
var increment = function(value) {
count += value;
console.log(count);
}
// return as an object with reference to the functions
return {
counterIncrement : counterIncrement,
increment : increment
}
}
var v= Counter();
v.counterIncrement();
v.increment(100);
---- Previous Answer ------
When you execute var v=Counter();
, essentially you are executing the function Counter()
and assigning its return value to v
.
In this case, Counter()
would return a reference to the function counterIncrement
.
So variable v
now holds a callable function named counterIncrement()
. When you do v.counterIncrement()
, your javascript engine sees the code as counterIncrement.counterIncrement()
, which gets resolved into an undefined property. This produces the error message that you were seeing.
Calling just v()
is sufficient.
function Counter()
{
var count=0;
var counterIncrement=function()
{
count++;
console.log(count);
}
return counterIncrement;
}
var v= Counter();
v();