Reputation: 1669
I am relatively new to JavaScript and I'm coming from a C# background. Interestingly enough, it looks like someone from earlier today is having the same problem I'm having: Serializing a Child Object Within a Parent Object in JavaScript
Basically, I can't figure out how to have instance-level methods either after an object has been through serialization. Is this even possible? If so, how? I am really interested in how someone can call the "test" method on a MyChild instance that is a property value on MyParent. If you can't do this, is it even fair to say JS supports OO? In my humble opinion, this just seems like a common scenario. I'm a bit surprised that I couldn't find an example online and instead stumbled on the post listed above.
Upvotes: 1
Views: 306
Reputation: 1775
Welcome to the world of JavaScript. But first things first, it would be good to understand that JavaScript isn't an OO based language. Actually JavaScript is a prototype based language, in which things changes a little in how it works, OO behavior is just simulated in this kind of language. Here I found a good question about it, and the explanations are very good and clear about what "JavaScript is". Link to the question. There are other languages like LUA, or a more recent one IOLanguage that are prototype based languages as well. Wikipedia have a good article about it with a list of prototype based languages.
I thought that it would be important to know these things if you are really trying to learn JavaScript, this may clear some things up.
Now about your question, it isn't very clear whether the question is just about the serialization or about how JavaScript "OO" works. Anyway it seems that the confusion is in this part of the code of the example that you posted, and yes it could be confusing when you are just learning JS.
The part of the code with comments clarifying some points:
//The function declaration that will become the constructor
//There is a reason for the "why" the function is used as the "base prototype"
//for other objects, but this would be a little out of context.
function MyChild() { this.init(); }
//Here you start declaring the prototype of instances from MyChild
//So everything that is in the prototype are like static (from C#) references
//in the objects, for properties, not for functions
MyChild.prototype = {
//Here is the problem of the code of the question that you posted
//The problem is that, when you create a instance from MyChild,
//"data" isn't a instance field, which means that if you change it,
//it will change in every object created from MyChild, this is because
//it is the same reference in all of them
data: {
id: 0,
fullName: "",
},
//Here we have the methods of the instance, when they are called, "this"
//is the object itself, so to access fields of the object you must use "this"
//in order to contextualize the operation, without "this" you are going to access
//the current function/global scope
init: function () {
//since the constructor call this method we can initialize things here
//these are the instance fields, you must set the property in the "this"
this.prop1 = 1;
this.prop2 = 2;
},
save: function (key) {
},
load: function (key) {
},
test: function () {
alert("Testing Child functions");
}
}
Somethings that would be good to know to fully understand JavaScript easily:
Upvotes: 2