33yo, studying to be a front-end developer(eyeballing fullstack for the future).
goal is to develop and sell web and mobile solution packages to companies somewhere down the line.
But in my initial code I had methods to change both size and location. Is it possible to to still have those methods if I use let instead of const? (It was part of the exercise to have those methods)
Ah, yes. Now we have reached the intricacies of JavaScript. When we assign a value to const, what we're really doing is we're just assigning a reference, not the actual value itself. I discuss the implication of this language feature in an old post of mine. 😉 How timely, isn't it?
As for adding methods for setting the size and location, we can simply add the usual setters and getters. We can do this because const stores references, not values.
functionRectangle(x,y,width,height){this.x=x;this.y=y;this.width=width;this.height=height;}// Rinse and repeat these setters and getters// for all propertiesRectangle.prototype.setX=function(newX){this.x=newX;}Rectangle.prototype.getX=function(){returnthis.x;}constrect=newRectangle(20,30,45,100);rect.getX();// Output: 20rect.setX(15);rect.getX();// Output: 15
33yo, studying to be a front-end developer(eyeballing fullstack for the future).
goal is to develop and sell web and mobile solution packages to companies somewhere down the line.
I can see some of the logic in this, ( we haven't
t touched on the subject of 'set & get' yet or prototyping but i will try to learn it in the coming week or two. thanks so much for taking the time to educate me :D
But in my initial code I had methods to change both size and location. Is it possible to to still have those methods if I use let instead of const? (It was part of the exercise to have those methods)
Ah, yes. Now we have reached the intricacies of JavaScript. When we assign a value to
const
, what we're really doing is we're just assigning a reference, not the actual value itself. I discuss the implication of this language feature in an old post of mine. 😉 How timely, isn't it?"Same Reference" vs. "Same Value"
Some Dood ・ Nov 5 '18 ・ 5 min read
As for adding methods for setting the size and location, we can simply add the usual setters and getters. We can do this because
const
stores references, not values.I can see some of the logic in this, ( we haven't
t touched on the subject of 'set & get' yet or prototyping but i will try to learn it in the coming week or two. thanks so much for taking the time to educate me :D
No problem at all! All I ask is for you to pay this forward in the future. As developers, we gotta look out for each other. 😉