简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM