Tag: Javascript

JavaScript: simple code structure for libraries

The code of a JavaScript library might get very complex over time. This can be a problem for maintenance and expandability.

When writing a library, you should address two main points:

  • Keep the environment clean from global variables and methods to avoid conflicts between different libraries
  • Provide public and private methods and variables

The following “template” provides a simple code structure that fulfills those points:

// Unique name of the library.
MyLibrary = function() {
  MyLibrary = {};

   * Internal variables and methods
  var variable = 'value';
  function doSomething() {
    // do something

   * Public variables and methods
  MyLibrary.myVariable = true;
  MyLibrary.myAction = function() {
    // do something

  // Return the library.
  return MyLibrary;

You can use such a library the following way:

const lib = MyLibrary();

if (lib.myVariable) {

Inspiration: https://stackoverflow.com/questions/13606188/writing-a-library-what-structure

Photo by Safar Safarov on Unsplash


Which programming language is the best one?

Is it really possible to answer this question?

Yes, the correct answer is: all of them. Or let’s ask another question first: for which problem?

There is not “the one and only” best programming language, there are tons. This question is simply not a good one. It’s good for endless discussion, but it’s neither complete, nor does it give you details about the problem to solve. An answer will not solve the unknown problem of the author. Every coding language has it advantages when it comes to solving a specific problem. But which one? There are advantages for JavaScript, Python, PHP, Java, Swift, Dart and others. But what if the author wants to write some firmware for a micro controller? Ok, you see the point.

But why do people ask those questions?

I see a lot of such questions posted on all social platforms. And as those questions are so incomplete, there are no correct answers. The results are endless discussions about the advantages of each coding language. And this is what most of the authors want: an open question which leads to endless attention. In detail: likes and retweets to pretend the algorithms to be an interesting account. Honestly, no one reads through thousands of responses. And if yes, you only see the advantages that each comfortable developer or user sees in his area of expertise. This might lead to some short attention, but it will never create some high quality and valuable content fo the users. And that’s a pity.

And what about: iOS or Android? Windows or macOS? Python or JavaScript? Same thing! The answers are just a time wasting mess.

Photo by Donald Giannatti on Unsplash.


AJAX: Asynchrones Senden eines HTTP-Requests

Zum Versenden eines asyncronen HTTP-Requests kann folgender Javascript-Code verwendet werden:

var req = new XMLHttpRequest();
req.onreadystatechange = handleStateChange;
req.open('GET', 'http://www.example.com/', true);
function handleStateChange() {
  switch (req.readyState) {
    case 0: // UNINITIALIZED
    case 1: // LOADING
    case 2: // LOADED
    case 3: // INTERACTIVE
    case 4: // COMPLETED
      handleResponse (req.status, req.responseText);
    default: // fehlerhafter Status
function handleResponse (status, responseText) {
  // verarbeiten der Daten ...

Werte für XMLHttpRequest.readyState:

  1. UNINITIALIZED – Noch nicht initialisiert, d.h. noch kein Aufruf von open().
  2. LOADING – Request wurde initialisiert, aber noch kein send().
  3. LOADED – Request wurde mittels send() abgesetzt.
  4. INTERACTIVE – Teile der Antwort sind bereits verfügbar. Auf die Daten kann über responseText zugegriffen werden.
  5. COMPLETED – Die Bearbeitung des Requests ist beendet.