2016-12-20 3 views
0

Я новичок в java, и мне не удается вызвать содержимое массива с конструктором. Я хочу конкретно называть содержимое в массив типа «omAttachment», «omText» и т.д.Array содержащий конструктор undefined js

здесь конструктор и массив:

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 

Когда я печать л ike this in console (google chrome) я получаю следующее:

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" 
+0

Я просто проверял свой код и он отлично работает! Даже в вашем примере это не печать неопределенной. – bman

ответ

0

Должно работать.

Рабочая Отрывок (Ваш код не отличается от этого?):

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("abc", "abc", "abc", "abc", "abc", "abc", "abc"); 
 
messageHistoryArray.push(newMessage); 
 
console.log(messageHistoryArray[0])

+0

У меня было это работает внутри функции init ... и вот почему я думаю, что это было немного фанки. вытащил его и теперь выглядит отлично. Спасибо! – gk103