I'm a newbie to Javascript and have question on closure functions.
In the below code, I'm creating a closure function and setting a new property firstname for the same function. I want to identify the property - firstname - of my function that is created and use the code - console.log(obj1.firstname);
to display it.
But for some reason its displaying as undefined. Please let me know what is the problem.
<html>
<head>
<script>
outerfunction = function(firstname,lastname) {
var innerfunction = function (sex) {
console.log(firstname);
console.log(lastname);
console.log(sex)
}
console.log('Initialize');
innerfunction.prototype.firstname = firstname;
return innerfunction;
}
obj1 = outerfunction('Bob','Mcdonald');
console.log(obj1.firstname);
obj2 = obj1('Male');
obj1 = outerfunction('Lucy','Mary');
obj1('Female');
</script>
</head>
<body>
This is the body
</body>
</html>