emailconfirmed, nsInternRO, nsInternRW, Administrators
3,356
edits
m (→Funktionen) |
(Added best practices) |
||
Line 203: | Line 203: | ||
== Klassen und Objekte == | == Klassen und Objekte == | ||
JavaScript ist | JavaScript ist objektbasierte Programmiersprache (und gehört im weiteren Sinn zu den objektorientierten Programmiersprachen). In JS ist quasi alles ein Objekt bzw. eine Klasse. Der Unterschied zwischen einer Klasse und einem Objekt ist, dass man die Vorlage eines Objektes ("Blueprint") als ''Klasse'' bezeichnet. Von einer Klasse können beliebig viele "Instanzen" erstellt werden. Wenn mit dem Schlüsselwort <tt>new MyClass()</tt> eine Instanz einer Klasse erstellt wurde, spricht man von einem Objekt. | ||
Vereinfacht heißt das: Ein Objekt existiert | Vereinfacht heißt das: Ein Objekt (also eine Instanz einer Klasse) existiert "lebendig" und veränderbar im Speicher des Rechners, eine Klasse hingegen existiert nicht (im Sinne einer "lebendigen Veränderbarkeit") bzw. nur unveränderlich als Quellcode ("Blueprint"/Vorlage) bzw. Textdatei auf der Festplatte. | ||
Z.B. erstellt man ein neues ''Objekt'' <tt>myDate</tt> aus der ''Klasse'' <tt>Date</tt> so: <source lang="JavaScript">var myDate = new Date();</source> | Z.B. erstellt man ein neues ''Objekt'' <tt>myDate</tt> aus der ''Klasse'' <tt>Date</tt> so: <source lang="JavaScript">var myDate = new Date();</source> | ||
Line 267: | Line 267: | ||
Klassen fangen im Gegensatz zu Variablen üblicherweise mit einem Großbuchstaben an. Vgl. auch [[CamelCase]] | Klassen fangen im Gegensatz zu Variablen üblicherweise mit einem Großbuchstaben an. Vgl. auch [[CamelCase]] | ||
== Best Practices == | |||
JavaScript ist eine komplexe Sprache mit noch komplexeren Möglichkeiten. Für kleine, einfache Aufgaben kann man die Informationen auf dieser Seite bereits gut nutzen. Für umfangreichere Projekte sollte man ein paar wichtige Grundsätze beachten: | |||
* Benutze kein JavaScript für elementare Seitennavigation oder zur Darstellung wichtiger Informationen | |||
** Gültig vor allem bei universell abrufbaren Webseiten; eingeschränkt gültig für sog. "WebApps" | |||
** Grundlegende Funktionen/Informationen der Webseite sollten auch in Browsern ohne JavaScript (-> [[Barrierefreiheit]]) oder bei bewusst deaktivertem JavaScript (z.B. aus Sicherheitsgründen) erreichbar sein | |||
** "Add functionality with JavaScript, don’t create too much content" | |||
* Bennene Deine Variablen und Funktionen sinnvoll und treffend | |||
** Halte Dich an Bennenungskonventionen (z.B. Klassen beginnen mit Großbuchstaben, Variablen werden klein geschrieben) | |||
* Vermeide globale Variablen | |||
* Kommentiere Quellcode sinnvoll: so viel wie nötig, so wenig wie möglich | |||
* Gewöhne Dir einen konsistenten Programmierstil an | |||
* Strukturiere Deinen Code modular – eine Funktion pro Aufgabe | |||
* Verbessere Dein Programm progressiv | |||
* Test early, test often | |||
* Erlaube andere Konfigurationen und Übersetzungen | |||
* Optimiere Schleifen | |||
* Optimiere nicht für spezielle Browser | |||
* Don’t trust any data | |||
* Erfinde nicht das Rad neu | |||
* Bekämpfe nicht das Framework ("Don't fight the framework") | |||
** Informiere Dich über die "Design Patterns" (Programmierkonzepte) der Programmiersprache | |||
* Komprimiere ("minify") JavaScripts und CSS-Dateien wenn sie in den produktiven Einsatz gehen | |||
** Sei ressourcenschonend | |||
* Stelle Deinen Code unter eine (freie) Lizenz | |||
Einige Punkte dieser Aufzählung basieren auf dem unbedingt lesenwerten Artikel (engl:) [http://dev.opera.com/articles/view/javascript-best-practices/ JavaScript Best Practices] auf dev.opera.com, der viele Punkte detailliert erklärt und konkrete (Code-) Beispiele aufzeigt. | |||
== Debugging == | == Debugging == |