0

I think that I've missunderstood the use of the keyword 'this' in javascript. I can't get my head around why "this.box1" changes from [object Object] to [object HTMLDivElement] the second time the mainloop gets executed, and why the TypeError gets thrown?

"box1" is the html id for a div element in the html code, that is shaped like a square.

function GeoObject(id){
    this.name = id;
    this.object = document.getElementById(id);
    this.objectStyle = window.getComputedStyle(this.object);
    this.heightCenter = parseInt(this.objectStyle.height) / 2;
    this.widthCenter = parseInt(this.objectStyle.width) / 2;
    console.log(this.name + "'s heightCenter: " + this.heightCenter);
    console.log(this.name + "'s widthCenter: " + this.widthCenter);
}

GeoObject.prototype.setMiddle = function(){
    //console.log("In setmiddle, this.heightCenter: " + this.heightCenter);
    this.object.style.top = (window.innerHeight / 2) - this.heightCenter + "px";
    this.object.style.left = (window.innerWidth / 2) - this.widthCenter + "px";
    console.log(this.name + "'s top: " + this.object.style.top);
    console.log(this.name + "'s left: " + this.object.style.left);
}

function PlayGround(){
    this.box1 = new GeoObject("box1");
    console.log("PlayGround.box1 object: " + this.box1.object);
    console.log("PlayGround.box1 objectStyle " + this.box1.objectStyle);
}


PlayGround.prototype.update = function(){
    console.log("this.box1.object: " + this.box1.object);
    this.box1.setMiddle();
    that = this;
    requestAnimationFrame(that.update);
};

P1 = new PlayGround();
//console.log("P1.box1.object: " + P1.box1.object);
P1.update();

Output:

"box1's heightCenter:" 100                                main.js (row 21)
"box1's widthCenter:" 100                                 main.js (row 22)
"PlayGround.box1 object:" [object HTMLDivElement]         main.js (row 35)
"PlayGround.box1 objectStyle" [object CSS2Properties]     main.js (row 36)
"this.box1:" [object Object]                              main.js (row 41)
"box1's top": 197px                                       main.js (row 29)
"box1's left": 583px                                      main.js (row 30)
"this.box1:" [object HTMLDivElement]                      main.js (row 41)

TypeError: this.box1.setMiddle is not a function        main.js (row 42, col 4)
this.box1.setMiddle();
PandaDeTapas
  • 516
  • 1
  • 7
  • 16

0 Answers0