Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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". <a href="https://stackoverflow.com/questions/186244/what-does-it-mean-that-javascript-is-a-prototype-based-language">Link to the question.</a> There are other languages like LUA, or a more recent one IOLanguage that are prototype based languages as well. <a href="http://en.wikipedia.org/wiki/Prototype-based_programming" rel="nofollow noreferrer">Wikipedia have a good article about it with a list of prototype based languages.</a></p> <p>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.</p> <p>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.</p> <p>The part of the code with comments clarifying some points:</p> <pre><code>//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"); } } </code></pre> <p>Somethings that would be good to know to fully understand JavaScript easily:</p> <ul> <li>Prototype paradigm/Prototype based languages, how the prototype works (is more about cloning than instantiating)</li> <li>Declarations using "var" or not</li> <li>Scope of things, in JavaScript scope is function based not "{ ... }" based</li> <li>Context, "this" is accessible throughout all the JavaScript code, but it means different things in different places. Also "this" can be "set" by calling some special methods in JS.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload