highwingers
highwingers

Reputation: 1669

Javascript property returns undefined

function ordersUtil() {

    var obj = {

        currentPage: 1
    }

    return obj;
}


console.log(ordersUtil.currentPage);

console.log(ordersUtil.currentPage); returns undefined.

I am setting up default property within my javaScript object literal, however why cant I simply access it? I know I can do ordersUtil.currentPage = 1 after my object and then i will have its value. my question is how do I setup default values and access them?

Upvotes: 3

Views: 180

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123563

It's undefined because the function itself doesn't have that property. If you called it, the Object that's returned, however, would:

var orders = ordersUtil();

console.log(orders.currentPage);

The function could hold the property, as you found out:

I know I can do ordersUtil.currentPage = 1 after my object [...]

But, it doesn't currently.

Upvotes: 2

Related Questions