Klassen und Objekte in JavaScript: Unterschied zwischen den Versionen
Aus Wikizone
(Die Seite wurde neu angelegt: „https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript == Klassen in JavaScript == In JS gibt es '''kein ''class'' Sch…“) |
|||
| Zeile 10: | Zeile 10: | ||
var person1 = new Person(); | var person1 = new Person(); | ||
var person2 = new Person(); | var person2 = new Person(); | ||
| + | </pre> | ||
| + | |||
| + | === Klassen in eigenem Namespace === | ||
| + | Beispiel: | ||
| + | <pre> | ||
| + | var MyApp = MyApp || function(initObj){ | ||
| + | this.config = initObj.config | ||
| + | this.otherstuff = initObj.otherstuff | ||
| + | }; | ||
| + | </pre> | ||
| + | |||
| + | == Eigenschaften in Klassen == | ||
| + | <pre> | ||
| + | var Person = function (firstName) { | ||
| + | this.firstName = firstName; | ||
| + | console.log('Person instantiated'); | ||
| + | }; | ||
| + | |||
| + | var person1 = new Person('Alice'); | ||
| + | var person2 = new Person('Bob'); | ||
| + | |||
| + | // Show the firstName properties of the objects | ||
| + | console.log('person1 is ' + person1.firstName); // logs "person1 is Alice" | ||
| + | console.log('person2 is ' + person2.firstName); // logs "person2 is Bob" | ||
| + | </pre> | ||
| + | |||
| + | == Methoden in Klassen == | ||
| + | <pre> | ||
| + | var Person = function (firstName) { | ||
| + | this.firstName = firstName; | ||
| + | }; | ||
| + | |||
| + | Person.prototype.sayHello = function() { | ||
| + | console.log("Hello, I'm " + this.firstName); | ||
| + | }; | ||
| + | |||
| + | var person1 = new Person("Alice"); | ||
| + | var person2 = new Person("Bob"); | ||
| + | |||
| + | // call the Person sayHello method. | ||
| + | person1.sayHello(); // logs "Hello, I'm Alice" | ||
| + | person2.sayHello(); // logs "Hello, I'm Bob" | ||
</pre> | </pre> | ||
Version vom 5. Dezember 2014, 15:36 Uhr
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
Klassen in JavaScript
In JS gibt es kein class Schlüsselwort. Stattdessen nimmt man einfach eine Funktion:
var Person = function () {
console.log('instance created');
};
var person1 = new Person();
var person2 = new Person();
Klassen in eigenem Namespace
Beispiel:
var MyApp = MyApp || function(initObj){
this.config = initObj.config
this.otherstuff = initObj.otherstuff
};
Eigenschaften in Klassen
var Person = function (firstName) {
this.firstName = firstName;
console.log('Person instantiated');
};
var person1 = new Person('Alice');
var person2 = new Person('Bob');
// Show the firstName properties of the objects
console.log('person1 is ' + person1.firstName); // logs "person1 is Alice"
console.log('person2 is ' + person2.firstName); // logs "person2 is Bob"
Methoden in Klassen
var Person = function (firstName) {
this.firstName = firstName;
};
Person.prototype.sayHello = function() {
console.log("Hello, I'm " + this.firstName);
};
var person1 = new Person("Alice");
var person2 = new Person("Bob");
// call the Person sayHello method.
person1.sayHello(); // logs "Hello, I'm Alice"
person2.sayHello(); // logs "Hello, I'm Bob"