[S3L4] Classes Flashcards

1
Q

Sind Klassen in Javascript so wie Klassen in anderen Sprachen?

A

-Nein

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sind Klassen in Javascript nativ vorhanden?

A
  • Nein, sie sind nur syntactic sugar.

- Sie sind Constructors und Prototypes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ist Javascript eine native Objektorientierte Sprache OOP?

A

-Nein, mit ES6 wurden Klassen nur sehr vereinfacht

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Was sind Klassen in Javascript?

A
  • Sie sind Constructors und Prototypes

- Spezielle Funktionen (Wie Arrow Functions besonders sind)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Wie richtet man eine Klasse ein?

A
clas Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}

const newRect = new Rectangle(400, 800);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Was macht die Constructor Function in einer Class?

A
  • Ist der Kleber, welche alles zusammenbindet

- Alle direkten Attribute/Properties werden im Constructor gesetzt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Muss man sowas wie Parent.call(this, bananaAttrs) mit Classes noch verwenden?

A

-Nein, braucht man nie wieder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Was macht die extends Methode?

A

-Ersetzte das .call(this, someAttrs); in Zukunft

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

was macht die super() Function?

A

-Ersetzte jegliche andere bishere Syntax um Objects und Prototypes zu verbinden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Sind Klassen nur Funktionen?

A

-JA!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Erkläre die Klasse:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
speak() {
console.log(this.name + ' barks.');
}
}
A
  • Animal ist eine Klasse
  • Animal hat eine Klassenmethode speak()
  • extends erweitert eine Klasse
  • super(name) wird den Namen an die Animal Class hochgeben (Binding mit .call und Prototyp Binding)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Was macht super ()?

A

-Gibt die Attribute an die Oberklasse weiter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Wenn man eine Klasse extended muss man super() benutzen?

A

Ja, sonst erhält man keinen Zugang zu den Attributen!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Gebe ein Beispiel einer Class an:

A
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
}
}
class Cat extends Animal {
constructor(name) {
super(name);
}
speak() {
console.log(this.name + ' meows.');
}
}
}

const sascha = new Dog(‘Sascha’);

coinsole. log(sascha );
sascha. speak(); //funktioniert, weil durch die Inheritance die speak() Methode reachable is

const thiara= new Cat('Thiara');
thiara.speak(); //thiara meows because she overrides the base method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Was für Vorteile bieten Klassen?

A
  • Einfacher zu benutzen
  • Inheritance is easy to manage
  • Cleaner code
  • Prototype system is native/free
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Wie wandelt man diese Person Constructor Function in eine Person class um?

function persin(attributes) {
this.age = attributes.age;
this.name = attributes.name;
this.homeTown = attributes.homeTown;
}

Person.prototype.speak = function () {
return Hello, my name is $[this.name};
};

A
class Person {
constructor(attributes) {
this.age = attributes.age;
this.name = attributes.name;
this.homeTown = attributes.homeTown;
}
speak() {
return `Hello, my name is $[this.name}`;
}
}
17
Q

Wie sieht eine Klasse mit super() und neuen Attributen aus?

A
class Child extends Parent {
construcotr(childAttrs) {
super(childAttrs); //THIS IS IMPORTANT
this.isChild = childAttrs.isChild; //UNIQUE ATTRIBUTE
}
checkIfChild() {
if(this.isChild) {
console.log(this.speak+ ' and I am a child object.');
}
}