How can i add an id to the constructor?

function ready() { function Rectangle(i, w, h, c) { this.id = i; this.width = w; this.height = h; this.color = c; } Rectangle.prototype.elemId = function() { return this.id = document.getElementById(); } Rectangle.prototype.doublewidth = function() { return this.width * 2; } Rectangle.prototype.doubleheight = function() { return this.height * 2; } var oneRect = new Rectangle("elem", 300, 150, "green"); oneRect.elemId().style.background = oneRect.color; oneRect.elemId().style.width = oneRect.doublewidth() + "px"; oneRect.elemId().style.height = oneRect.doubleheight() + "px"; }; document.addEventListener("DOMContentLoaded", ready); 
 <div id="elem"></div> 

Closed due to the fact that the essence of the issue is incomprehensible to the participants Pavel Mayorov , Denis Bubnov , aleksandr barakin , Vadim Ovchinnikov , user194374 3 Feb '17 at 6:07 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    It is not clear what the code should do, and what is the relationship between id and constructor. You already pass id to the constructor - Grundy
  • question stumps - Sublihim
  • Perhaps the correct answer is here: Да, именно так - vp_arth
  • A little offtopic. If you plan to change the properties of a rectangle through the style - your object will become obsolete with each such operation. Define these operations inside the class. - vp_arth
  • Can you please elaborate a bit about the style :) I just just started to learn and do not understand a bit about the styles that you wrote - Dima Vleskov

3 answers 3

id has already been added to your constructor: this.id = i

Apparently, you are trying to return an element by id in the elemId method:

 Rectangle.prototype.elemId = function() { return document.getElementById(this.id); } 

    In the same constructor, you can create a field where the link to the html-объект will be stored, because each time calling getElementById not very correct

     function ready() { function Rectangle(i, w, h, c) { this.id = i; this.width = w; this.height = h; this.color = c; // ссылка на html объект this.elem = document.getElementById(i); } Rectangle.prototype.doublewidth = function() { return this.width * 2; } Rectangle.prototype.doubleheight = function() { return this.height * 2; } var oneRect = new Rectangle("elem", 300, 150, "green"); oneRect.elem.style.background = oneRect.color; oneRect.elem.style.width = oneRect.doublewidth() + "px"; oneRect.elem.style.height = oneRect.doubleheight() + "px"; }; document.addEventListener("DOMContentLoaded", ready); 
    • Good advice, but I want to note that that's just getElementById one of the most optimized methods for working with DOM. It is quite possible to call it yourself again. - vp_arth

    An example of implementation through setters (described only the width for simplicity):

     function Rectangle(id, w, h, color) { var el = document.getElementById(id); el.style.width = w + 'px'; el.style.height = h + 'px'; el.style.backgroundColor = color; Object.defineProperty(this, "width", { set: function (width) { w = width; el.style.width = w + 'px'; }, get: function() {return w = parseFloat(el.style.width);} }); } var r = new Rectangle('rect', 25, 50, 'red'); setTimeout(() => r.width*=2, 5e2); setTimeout(() => r.width*=2, 10e2); setTimeout(() => r.width*=2, 15e2); 
     <div id="rect"></div> 

    • one
      thanks, now it became clear - Dima Vleskov
    • And you know that in the current view in your code, new optional. And if you remove it, nothing will change? - Grundy
    • one
      No need to remove anything) Tomorrow we will change the implementation, say on the new-fashioned class =) - vp_arth
    • Then it will work differently. In this case, your Rectangle is not a constructor, but a normal function - Grundy
    • @Grundy - any function - a potential constructor. But also any constructor is a common function. The function in the constructor turns not the use of this , but the operator new The new operator describes well the behavior with functions like this. What's wrong? What exactly do you think will work differently? - vp_arth