Namanyay Goel
Namanyay Goel

Reputation: 2660

Revealing Module Pattern problems

Here's my code:

var APP = {}

APP.item = function() {
    var two = function() { return "three"; }
    return {
        two: two
    };
}

console.log(APP.item.two);

Now, from what I've read, shouldn't the output be "three"? Rather, the result is undefined.

Fiddle: http://jsfiddle.net/mhxpz/1/

Upvotes: 2

Views: 112

Answers (2)

Kevin Boucher
Kevin Boucher

Reputation: 16675

The result is undefined because you are not invoking the APP.item function (thus getting the return value. Also you will need to invoke the two function to get its return value:

console.log( APP.item().two() );  // outputs "three"

Upvotes: 2

guypursey
guypursey

Reputation: 3184

Both item and two are functions that need invoking:

console.log(APP.item().two());

...

EXTRA EXPLANATORY NOTE: In your original code, the item bit is just returning the function, not the object you want the function to return. Therefore when you ask for item.two it cannot find a two property (since that is part of the returned object, not part of the item function itself). Hope that makes sense of this for you.

Upvotes: 4

Related Questions