I have an array of objects inside an object.
Why do I get the error 'Cannot read property fname of undefined' below? How do I fix it?
Also, I'm new to javascript and would appreciate any styling or convention suggestions.
https://jsfiddle.net/go1eu9z6/
function myObj() {
this.X = [],
this.haveEntries = function() {
return this.A.length > 0;
},
this.add = function(fname_in, lname_in, city_in) {
var t = new Date().getTime() / 1000;
var obj = {
fname: fname_in,
lname: lname_in,
city: city_in,
t_create: t
};
this.X.push(obj);
return this;
}
}
var AB = {};
AB.X = new myObj();
AB.X.add("mike", 'smith', 'Toronto');
var s = AB.X[0].fname; // Error: Cannot read property fname of undefined
document.getElementById('fname').innerHTML = s
myObj, in your example. I think you meanvar AB = new myObj();