[英]Javascript property returns undefined
function ordersUtil() {
var obj = {
currentPage: 1
}
return obj;
}
console.log(ordersUtil.currentPage);
console.log(ordersUtil.currentPage); 的console.log(ordersUtil.currentPage); returns undefined. 返回undefined。
I am setting up default property within my javaScript object literal, however why cant I simply access it? 我在我的javaScript对象文字中设置默认属性,但为什么我不能简单地访问它? I know I can do ordersUtil.currentPage = 1 after my object and then i will have its value. 我知道我可以在我的对象之后执行ordersUtil.currentPage = 1然后我将拥有它的值。 my question is how do I setup default values and access them? 我的问题是如何设置默认值并访问它们?
It's undefined
because the function
itself doesn't have that property. 它是undefined
因为function
本身没有该属性。 If you called it, the Object
that's returned, however, would: 如果你调用它,那么返回的Object
将:
var orders = ordersUtil();
console.log(orders.currentPage);
The function
could hold the property, as you found out: 你发现,该function
可以保存属性:
I know I can do
ordersUtil.currentPage = 1
after my object [...] 我知道我可以在我的对象[...]之后执行ordersUtil.currentPage = 1
But, it doesn't currently. 但是,目前还没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.