xizor81
xizor81

Reputation: 1

Trouble understanding creating Objects from Classes contained within Private Closures

I'm a student just beginning and I'm having a repeating struggle with accessing/running an Object created via a Class from within closures. I grasp the idea of Class created Objects, but the moment they get wrapped in a private closure - something gets lost in translation for my brain.

I know that my problem in comprehending this has to be simple. I just need a little outside help.

I have two scenarios.

SCENARIO 1: Outputs : Woman {age: 20, grade: "B", class: "sophomore"}

//WORKING SCENARIO #1
var Woman = function(a, b, c){
        this.age = a;
        this.grade = b;
        this.class = c;
}

var diana = new Woman(20, 'B','sophomore');
console.log(diana);
//END WORKING SCENARIO

SCENARIO 2: Outputs: Woman {age: undefined, grade: undefined, class: undefined}

//WHAT I'M STRUGGLING WITH
var privateOne = (function(a,b,c){

    var Woman = function(a, b, c){
        this.age = a;
        this.grade = b;
        this.class = c;
    }

    var person = new Woman(a,b,c);

    return {
        showWoman : function(d,e,f){
            return person;
        }
    }

})();

var mary = privateOne.showWoman(20, 'B','sophomore');
console.log(mary);
//END WHAT I'M STRUGGLING WITH

Upvotes: 0

Views: 28

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370879

It sounds like you need the Woman to be instantiated when showWoman is called:

var privateOne = (function(a,b,c){
    var Woman = function(a, b, c){
        this.age = a;
        this.grade = b;
        this.class = c;
    }

    return {
        showWoman : function(a, b, c){
            return new Woman(a, b, c);;
        }
    }
})();

var mary = privateOne.showWoman(20, 'B','sophomore');
console.log(mary);

Upvotes: 1

Related Questions