Why is the variable without value?

  • 0
    Why js swears at this.carElem.css? when I call the draw method, I define it
    Cannot read property 'css' of undefined

    let Car = function(x, y){
    				this.x = x;
    				this.y = y;
    			};
    
    			Car.prototype.draw = function(){
    				this.carHTML = '<img src="1.png" height="60">';
    				this.carElem = $(this.carHTML);
    
    				this.carElem.css({
    					position: 'absolute',
    					left: this.x,
    					top: this.y
    				});
    
    				$('body').append(this.carElem);
    			};
    
    			Car.prototype.moveRight = function(){
    				this.x += 5;
    
    				this.carElem.css({
    					left: this.x,
    					top: this.y
    				});
    			};
    
    			let bmw = new Car(1,2);
    			let mercedes = new Car(200,300);
    
    			bmw.draw();
    			mercedes.draw();
    			let speedBmw = setInterval(bmw.moveRight, 1000);
    JavaScript Scarlet Burnett, Jan 3, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!