I'm currently in the learning process of JavaScript. I'm having a confusion with Objects (Reference type and plain objects). Here are some codes that create an object (reference type) :
function TheObject(first, last) {
this.first = first;
this.last = last;
}
TheObject.prototype.theMethod = function() {
document.write("first : " + this.first + ", last : " + this.last + "</br>");
};
var anObject = new TheObject("Google", "Good");
anObject.theMethod();
Here are some other codes which also create an object (is it also reference type?) :
var TheAnotherObject = function(first, last){
return {
first : first,
last : last,
theMethod : function() {
document.write("first : " + this.first + ", last : " + this.last + "</br>");
}
};
}
var anotherObject = TheAnotherObject("Yahoo", "Good");
anotherObject.theMethod();
Now, my confusion is where is the actual difference between this two way of creating objects. I know that I can create an Object type in both way (with the "new" keyword). Then what is the difference?
Please help me to understand what point I'm missing here. I know it's very important to understand since JavaScript heavily use functions and objects. Any help would be very much appreciated. Thanks in advance.
TheObjectclass.newas "classes", and then everyone will pretty much know what you are talking about.