-1

我创建了一个带有一些属性的 JavaScript 对象,并将一些值放入其中。

var summaryViewObject[transactionId] = new Object();
summaryViewObject[transactionId].paymentTotal = 0;
summaryViewObject[transactionId].numberOfTransactions = 0;
summaryViewObject[transactionId].returnedPayment = false;

当我们得到类似的属性时,returnedPayment它在 IE8 中显示错误,但在 Chrome、IE9 和 Firefox 等其他浏览器中工作正常。

请帮助我了解 JavaScript 对象和属性支持。

错误类型错误:无法获取属性“returnedPayment”的值:对象为空或未定义

4

2 回答 2

2

这一行:

var summaryViewObject[transactionId] = new Object();

无效,因为summaryViewObject未定义,您无法[transactionId]在定义之前在其上创建属性。

尝试这个:

var summaryViewObject = {};
summaryViewObject[transactionId] = {};
summaryViewObject[transactionId].paymentTotal = 0;
summaryViewObject[transactionId].numberOfTransactions = 0;
summaryViewObject[transactionId].returnedPayment = false;

而且,= new Object()= {};本质上是一样的。

于 2013-08-08T19:11:42.197 回答
0

这应该有效:

var transactionId = 0;
var summaryViewObject = new Array();
summaryViewObject[transactionId] = ({
    paymentTotal: 0,
    numberOfTransactions: 0,
    returnedPayment: true});

//These lines below are here to show you that you can now edit them like so:
summaryViewObject[transactionId].paymentTotal = 0;
summaryViewObject[transactionId].numberOfTransactions = 0;
summaryViewObject[transactionId].returnedPayment = false;

//should alert "false" even though we set it to true in the declaration
alert(summaryViewObject[transactionId].returnedPayment);
于 2013-08-08T19:14:21.613 回答