I had create two objects: User and Blog and I need to associate both of them. My thought was to create an ID for each and then make the association. The ID is created through array.length at the moment of creating an instance.
The associateUserWithBlog functions stores the blog with respective user. At user instance I define an array for store all blogs create by user. My question is if there is another way to do this association. I'll need later print all the blogs and its owner(user) and sometimes I struggling to print.
function User(name, level, title, id, entry, comment) {
this.name = name;
this.level = level;
this.title = title;
this.id = id;
this.entry = entry;
this.comment = comment;
}
function Blog(blogID, title, userID, blogText) {
this.blogID = blogID;
this.title = title;
this.userID = userID;
this.blogText = blogText;
this.comment = comment;
}
users = new Array();
blogs = new Array();
function associateUserWithBlog(userID, entryID) {
users[userID].entry = entryID;
}
// user with id = 0
userID = users.length
var user = new User(userID, "Bob", "Student", "Mr", new Array())
// blog with id = 0 and user associate with id = 0
blogID = blogs.length
var blog = new Blog(entries.length, "Blog 1", 0, "Welcome Blog 1");
associateUserWithEntry(userID, blogID);
Blog.user = new User('name', 'level', ...).