var myName = {
name : 'Rakesh',
yearOfBirth : '1981',
calculateAge : function(){
console.log(this);
console.log(2016 - this.yearOfBirth);
function innerFunction(){
console.log("Hello World");
}
innerFunction();
}
}
Asked
Active
Viewed 80 times
-2

Tushar
- 85,780
- 21
- 159
- 179

user1613478
- 29
- 5
-
`myName.calculateAge()` will call the `calculateAge` method and `innerFunction();` at the end of it will call `innerFunction`. – Tushar Dec 28 '16 at 11:23
-
Possible duplicate of http://stackoverflow.com/questions/7295634/javascript-nested-function – Akshay Tilekar Dec 28 '16 at 11:25
2 Answers
0
Javascript has function scope. So, normally you can't access innerFunction() in javascript from out side.
Unless you attach that function to an object outside.
var dummy = {};
var myName = {
name : 'Rakesh',
yearOfBirth : '1981',
calculateAge : function(){
console.log(this);
console.log(2016 - this.yearOfBirth);
function innerFunction(){
console.log("Hello World");
};
innerFunction();
dummy.innerFunction = innerFunction;
}
}
// Then you can do
myName.calculateAge(); // After you run this line, you will have dummy.innerFunction available
dummy.innerFunction();
That is a very dumb way to do it.

vothaison
- 1,646
- 15
- 15
0
There are many ways to do this, one would be -
var myName = {
name: 'Rakesh',
yearOfBirth: '1981',
calculateAge: function() {
console.log(this);
console.log(2016 - this.yearOfBirth);
function innerFunction() {
console.log("Hello World");
};
return innerFunction;
}
}
myName.calculateAge()();

Dhruv
- 173
- 11