> ES5 implements classes
Well, there's your first hurdle. Maybe you should consider starting with ES6, it has classes.
The real problem is the name *Java*Script. Javascript has fuck all to do with Java. The change from LiveScript to "Java"Script was some marketing ploy to increase adoption.
My advice: Treat Javascript like a completely new language (to you) and start with ES6. You'll need to know about Babel or another downpiler if you are writing code that needs to run in browsers.
I agree that being explicit is more important here.
The best solution is probably to just use the setter and getter (AKA properties) support built into JS itself. That way your properties look like normal properties for the users, but you can still handle setting and getting with your own code.
http://javascriptplayground.com/blog/2013/12/es5-getters-setters/
Here's some helpful links on the matter, then:
https://spring.io/understanding/javascript-promises
http://javascriptplayground.com/blog/2015/02/promises/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Ah, I see now. Thanks for the clarification.
Edit: Found better information, for those interested.
> Can someone explain why this isn't working
It's very probably an asynchronous call. The body of that function(data) {} function is only executed when the download completes. So that document.write is actually executed before it's set.
> and give me some suggestions?
There's no simple answer: you need to structure your application around async callbacks. Some links that might be of use:
http://javascriptplayground.com/blog/2013/06/think-async/
http://www.html5rocks.com/en/tutorials/async/deferred/
Prototype is nice for those cases where you'd want to create many instances of a class, or where you feel like having an extendable class.
But in any other case, where you just need one instance for a specific task, JS's object shorthand is much more convenient:
var nameHandler = { names: [], add: function(name) { this.names.push(name); } };
[edit]: AndrewJM also has a point, with prototypes your code can be a nice and flat list of components that can be included as you go, where it otherwise might be a messy hierarchial thing stored in one big blob. Whichever you choose, make sure to read through this article on objects and variable scope to avoid some object-associated headaches.
For ES5, you can't. JS is a different language from C++ and you should not try and force it to be something it's not. You're just gonna have to utilize one of the many JavaScript inheritance methods. If you haven't already, spend some time googling for methods because there's more than one. Start here.
ES6 introduces classes and a more familiar way of writing inheritance. See here.
But you HAVE to understand prototyping either way, as you will get lost and stuck eventually if you cannot.