new
Ange värdet på utrycket nedan.
1 function constructor() {this.a=1}; let o = new constructor(); o.a
2 function constructor(x) {this.a=1; this.b=x}; let o = new constructor(20); o.b
3 function constructor(x, y) {this.a=2; this.b=x; this.c=x*y}; let o = new constructor(20,10); o.a+o.b+o.c
4 function constructor() {this.a=1; return {a:2}}; let o = new constructor(); o.a
🪧 Vi leker med en konstruktor
I uppgifterna nedan utgå från att de utförs efter varandra.
function student(name) {
this.name=name;
this.point = 0;
this.label = function() {
return this.name + ": " + this.point + "p"
}
this.add = function(x) {
this.point = this.point + x;
}
};
let kalle = new student("Kalle");
let kajsa = new student("Kajsa");
9 Beskriv med egan ord vad kommandor "new" gör för att skappa ett objekt.
10 Vad används objektet *this* till?
Faci
- 1
- 20
- 222
- 2
- Reflektion 💭
- Reflektion 💭