包含构造函数的数组是未定义的js

时间:2016-12-20 23:07:38

标签: javascript arrays

我是java的新手,我无法调用具有构造函数的数组的内容。我想专门调用类型为" omAttachment"," omText"的数组中的内容。等

这是构造函数和数组:

function observeMessages(attachment, read, senderId, sent, text, type, userName){
  this.omAttachment = attachment;
  this.omRead = read; 
  this.omSenderId = senderId;
  this.omSent = sent;
  this.omText = text; 
  this.omType = type;
  this.omUserName = userName;
}

var messageHistoryArray = [];

我添加了这样的构造函数:

var newMessage = new observeMessages(obj.attachment, obj.read, obj.senderId, obj.sent, obj.text, obj.type, obj.userName);
      messageHistoryArray.push(newMessage);

当我尝试在控制台中打印时,它表示未定义:

console.log(messageHistoryArray[0]); /in js file

当我在控制台(谷歌浏览器)中这样打印时,我得到以下内容:

console.log(messageHistoryArray); //in js file

//print out in chrome console
Array[0]
     0:observeMessages
     omAttachment:false
     omRead:"read"
     omSenderId:"randomSenderId"
     omSent:1479573310.904605
     omText:"random text stuff"
     omType:"type"
     omUserName:"userName"

1 个答案:

答案 0 :(得分:0)

它应该有用。

Working Snippet(你的代码与此有什么不同吗?):

view.php