how to build a rectangle using a constructor, it seems like I wrote everything, but where is the error.

function ready() { function Rectangle(w, h) { this.width = w; this.height = h; } Rectangle.prototype.width = function() { return this.width * 2; } Rectangle.prototype.height = function() { return this.height * 2; } var rectOne = new Rectangle(300, 200); var test = document.getElementById("test"); test.style.background = "red"; test.style.width = rectOne.width() + "px"; test.style.height = rectOne.height() + "px"; }; document.addEventListener("DOMContentLoaded", ready); 
 <div id=test></div> 

    1 answer 1

    When you say "It does not work for me, help!", It would be nice to write what says something that does not work (error message).

    You have properties width and height .
    In the prototype there are functions with the same names.

    Properties / methods are first searched in the object and, if they are not found locally, only then in the prototype chain.
    Immediately we immediately stumble upon the width , which no one will continue to search for a property.
    Replace method names with getWidth , for example.

    • thanks, corrected, earned - Dima Vleskov