Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 879
highwingers Avatar asked Jun 12 '26 17:06

highwingers


1 Answers

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.

like image 111
Jonathan Lonowski Avatar answered Jun 15 '26 06:06

Jonathan Lonowski