Skip to content

Commit dd300d7

Browse files
Kapitel 3
1 parent a186cf6 commit dd300d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+694
-83
lines changed

Kapitel3/3_Emulieren_von_Klassen.js

-9
This file was deleted.

Kapitel3/3_Prototypen.js

-38
This file was deleted.

Kapitel3/3_private_members.js

-36
This file was deleted.

Kapitel3/Listing_3-1.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var max = {
2+
name : 'Max',
3+
nachname : 'Mustermann',
4+
sagHallo : function() {
5+
console.log('Hallo');
6+
}
7+
}

Kapitel3/Listing_3-10.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Person {
2+
constructor(vorname, nachname) {
3+
this.vorname = vorname;
4+
this.nachname = nachname;
5+
}
6+
toString() {
7+
return this.vorname + ' ' + this.nachname;
8+
}
9+
}

Kapitel3/Listing_3-11.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Person {
2+
constructor(vorname, nachname) {
3+
this.vorname = vorname;
4+
this.nachname = nachname;
5+
}
6+
toString() {
7+
return this.vorname + ' ' + this.nachname;
8+
}
9+
}
10+
var max = new Person('Max', 'Mustermann');
11+
console.log(max.toString());

Kapitel3/Listing_3-12.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var max = {
2+
name: 'Max',
3+
nachname: 'Mustermann'
4+
};
5+
console.log(max.__proto__); // Object {}
6+
console.log(Object.getPrototypeOf(max)); // Object {}

Kapitel3/Listing_3-13.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var max = {
2+
name: 'Max',
3+
nachname: 'Mustermann'
4+
};
5+
var maexchen = Object.create(max);
6+
maexchen.name = "Maexchen";
7+
console.log(maexchen.__proto__);
8+
// Object {name: "Max", nachname: "Mustermann"}
9+
console.log(Object.getPrototypeOf(maexchen));
10+
// Object {name: "Max", nachname: "Mustermann"}
11+
console.log(maexchen.name); // Maexchen
12+
console.log(maexchen.nachname); // Mustermann

Kapitel3/Listing_3-14.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function Film(titel, produktionsjahr) {
2+
this.titel = titel;
3+
this.produktionsjahr = produktionsjahr;
4+
};
5+
var spiderman = new Film('Spiderman', 2002);
6+
var starWars = new Film('Star Wars', 1977);
7+
console.log(spiderman.__proto__); // Film {}
8+
console.log(starWars.__proto__); // Film {}
9+
console.log(Object.getPrototypeOf(spiderman)); // Film {}
10+
console.log(Object.getPrototypeOf(starWars)); // Film {}
11+
console.log(spiderman.constructor); // function Film() {...}
12+
console.log(starWars.constructor); // function Film() {...}

Kapitel3/Listing_3-15.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var hund = {
2+
name: undefined,
3+
bellen: function() {
4+
console.log('Wau');
5+
}
6+
}
7+
var bello = Object.create(hund);
8+
bello.name = 'Bello';
9+
var struppi = Object.create(hund);
10+
struppi.name = 'Struppi';
11+
hund.bellen() // Wau
12+
bello.bellen(); // Wau
13+
struppi.bellen(); // Wau

Kapitel3/Listing_3-16.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var tier = {
2+
name: undefined,
3+
fressen: function() {
4+
console.log('fressen');
5+
}
6+
}
7+
var hund = Object.create(tier);
8+
hund.bellen = function() {
9+
console.log('Wau');
10+
}
11+
var bello = Object.create(hund);
12+
bello.name = 'Bello';
13+
var struppi = Object.create(hund);
14+
struppi.name = 'Struppi';
15+
bello.fressen(); // fressen
16+
bello.bellen(); // Wau

Kapitel3/Listing_3-17.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var tier = {
2+
fressen: function() {
3+
console.log('fressen');
4+
}
5+
}
6+
var hund = Object.create(tier);
7+
hund.bellen = function() {
8+
console.log(this.name + ': Wau');
9+
}
10+
var bello = Object.create(hund);
11+
bello.name = 'Bello';
12+
var struppi = Object.create(hund);
13+
struppi.name = 'Struppi';
14+
var anonymerHund = Object.create(hund);
15+
anonymerHund.bellen(); // undefined: Wau
16+
bello.bellen(); // Bello: Wau
17+
struppi.bellen(); // Struppi: Wau

Kapitel3/Listing_3-18.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var tier = {
2+
fressen: function() {
3+
console.log('fressen');
4+
}
5+
}
6+
var hund = Object.create(tier);
7+
hund.bellen = function() {
8+
console.log(this.name + ': Wau');
9+
}
10+
var bello = Object.create(hund);
11+
bello.name = 'Bello';
12+
var struppi = Object.create(hund);
13+
struppi.name = 'Struppi';
14+
struppi.bellen = function() {
15+
hund.bellen();
16+
hund.bellen();
17+
}
18+
struppi.bellen(); // 2 x undefined: Wau

Kapitel3/Listing_3-19.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var tier = {
2+
fressen: function() {
3+
console.log('fressen');
4+
}
5+
}
6+
var hund = Object.create(tier);
7+
hund.bellen = function() {
8+
console.log(this.name + ': Wau');
9+
}
10+
var bello = Object.create(hund);
11+
bello.name = 'Bello';
12+
var struppi = Object.create(hund);
13+
struppi.name = 'Struppi';
14+
struppi.bellen = function() {
15+
hund.bellen.call(this);
16+
hund.bellen.call(this);
17+
}
18+
struppi.bellen(); // 2 x Struppi: Wau

Kapitel3/Listing_3-2.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var max = {
2+
name : 'Max',
3+
nachname : 'Mustermann',
4+
sagHallo : function() {
5+
console.log('Hallo');
6+
},
7+
haustier : {
8+
name : 'Bello',
9+
typ: 'Hund'
10+
}
11+
}

Kapitel3/Listing_3-20.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var tier = {
2+
fressen: function() {
3+
console.log('fressen');
4+
}
5+
}
6+
var hund = Object.create(tier);
7+
hund.bellen = function() {
8+
console.log(this.name + ': Wau');
9+
}
10+
var bello = Object.create(hund);
11+
bello.name = 'Bello';
12+
var struppi = Object.create(hund);
13+
struppi.name = 'Struppi';
14+
struppi.bellen = function() {
15+
console.log('Wau Wau');
16+
}
17+
bello.bellen(); // Bello: Wau
18+
struppi.bellen(); // Wau Wau

Kapitel3/Listing_3-21.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var Tier = {
2+
fressen: function() {
3+
console.log('fressen');
4+
}
5+
}
6+
var Hund = Object.create(Tier);
7+
Hund.bellen = function() {
8+
console.log(this.name + ': Wau');
9+
}
10+
var bello = Object.create(Hund);
11+
bello.constructor('Bello');
12+
var struppi = Object.create(Hund);
13+
struppi.constructor('Struppi');
14+
struppi.bellen = function() {
15+
Hund.bellen.call(this);
16+
Hund.bellen.call(this);
17+
}
18+
struppi.bellen(); // 2 x 'Struppi: Wau'

Kapitel3/Listing_3-22.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Schritt 1
2+
function Tier(name) {
3+
this.name = name;
4+
};
5+
// Schritt 2
6+
Tier.prototype.fressen = function() {
7+
console.log('fressen');
8+
}
9+
// Schritt 3
10+
function Hund(name, hunderasse) {
11+
Tier.call(this, name);
12+
this.hunderasse = hunderasse;
13+
};
14+
// Schritt 4
15+
Hund.prototype = new Tier();
16+
// Schritt 5
17+
Hund.prototype.constructor = Hund;
18+
// Schritt 6
19+
Hund.prototype.bellen = function() {
20+
console.log(this.name + ': Wau');
21+
}
22+
var bello = new Hund('Bello', 'Malteser');
23+
var struppi = new Hund('Struppi', 'Havaneser');
24+
struppi.bellen = function() {
25+
Hund.prototype.bellen.call(this);
26+
Hund.prototype.bellen.call(this);
27+
}
28+
bello.bellen(); // Bello: Wau
29+
struppi.bellen(); // 2 x Struppi: Wau

Kapitel3/Listing_3-25.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function extend(ziel, quelle) {
2+
ziel = ziel || {};
3+
for(var eigenschaft in quelle) {
4+
if(quelle.hasOwnProperty(eigenschaft)) {
5+
ziel[eigenschaft] = quelle[eigenschaft];
6+
}
7+
}
8+
return ziel;
9+
}

Kapitel3/Listing_3-26.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function extend(ziel, quelle) {
2+
ziel = ziel || {};
3+
for(var eigenschaft in quelle) {
4+
if(quelle.hasOwnProperty(eigenschaft)) {
5+
ziel[eigenschaft] = quelle[eigenschaft];
6+
}
7+
}
8+
return ziel;
9+
}
10+
var person = {
11+
name: 'Max',
12+
getName: function() {
13+
return this.name;
14+
}
15+
};
16+
var hund = {
17+
name: 'Bello',
18+
bellen: function() {
19+
console.log('Wau wau');
20+
}
21+
}
22+
extend(hund, person);
23+
console.log(hund.getName();

Kapitel3/Listing_3-27.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function extend(ziel, quelle) {
2+
ziel = ziel || {};
3+
for(var eigenschaft in quelle) {
4+
if(quelle.hasOwnProperty(eigenschaft)) {
5+
ziel[eigenschaft] = quelle[eigenschaft];
6+
}
7+
}
8+
return ziel;
9+
}
10+
var person = {
11+
name: 'Max',
12+
getName: function() {
13+
return this.name;
14+
}
15+
};
16+
var hund = {
17+
name: 'Bello',
18+
bellen: function() {
19+
console.log('Wau wau');
20+
}
21+
}
22+
extend(hund, person);
23+
person.getName = function() {
24+
console.log('getName() überschrieben');
25+
return this.name;
26+
}
27+
console.log(person.getName());// erst 'getName() überschrieben', dann 'Max'
28+
console.log(hund.getName()); // 'Bello'

0 commit comments

Comments
 (0)