Constructor functions and usage

So, i'm going over again this topic and just to clarify, i understand that when i do:
function User(name) {
this.name = name;

this.sayName = function() {
console.log(`Meu nome é ${this.name}`)
}
}
;
function User(name) {
this.name = name;

this.sayName = function() {
console.log(`Meu nome é ${this.name}`)
}
}
;
I'm like creating a function that will create a new object with all properties of the constructor, right? Something like that (sorry my bad english). And when i do:
let adam = new Usuario("adam");
let adam = new Usuario("adam");
Will already have the methods and properties referenced before but can someone give me a more complex and a real life example to when i will or how i can use that in a project itself? (personal or not) so i can understand fully the potential on how to work with Constructors.
8 Replies
kirplink
kirplink2y ago
I think reading more about object oriented programming would help you out. This give a pretty good overview of the principles of object oriented programming. https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/
freeCodeCamp.org
How to explain object-oriented programming concepts to a 6-year-old
by Alexander Petkov Have you noticed how the same cliche questions always get asked at job interviews — over and over again? I’m sure you know what I mean. For example: > Where do you see yourself in five years? or, even worse: > What do you consider to be
kirplink
kirplink2y ago
You use them when you need to have multiple objects that have the same methods, but different states. Think of all the enemies in a game. They all need an attack method and a take damage method, but they should have their own health pool. This way you can call enemy1.takeDamage() and you know it will only damage enemy1
ME
ME2y ago
Why don't you use classes?
loss
lossOP2y ago
got it, this will be a good resource and i can understand now with that example thanks! what do you mean? is a subject itself? I think is because i didn't get over there yet, i'm using JSInfo as a guide for know to keep studying but i'm in the object chapter i never learnt classes tho (i got superficial with stuff as assync, api's, modules and so but not classes still) is an advanced topic?
kirplink
kirplink2y ago
I do use classes. Constructors are the gateway drug to classes. but @boiithugger you need to understand the inner workings of JS to not fall into the trap of thinking you can use classes like you do in other programming languages. JS Classes don't have the encapsulation that you would expect in true object oriented languages At the end of the day the 'class' keyword in JS is syntactic sugar. It obfuscates what is really going on behind the scenes and implies that JS is doing something it is not. @lossw If you don't want to deal with classes at the moment I wouldn't worry about it I think it is more important to understand different principles of programming In this case understanding the principles of object oriented programming. What are the benefits it brings and what are the drawbacks, and when should you apply it to what you are working on.
loss
lossOP2y ago
i think i can understand why is important and how to make usage of it in a more complex way but sometimes, idk exactly how to solve some issues related to that not bcs of that but i just didn't got to this point yet, it was something that i hadn't came across yet prob i will get there soon
kirplink
kirplink2y ago
It will all come to you eventually My suggestion is focus on understanding when you should do/use something and not focus on syntax. Take a loop as an example. I think it is more important to know when you should use a loop and it is not as important to know how to write a loop. As long as you know what to use you can always google the syntax At the moment I have to jump between using JS, C#, and Python. There is no way I'm keep all the correct syntax in my head.
loss
lossOP2y ago
great, thank u guys (: this will make things easier tbh, sometimes i get a little lost on how to write or solve some problems that appears in coding platforms even tho i can understand and explain why/what's the concept i'm struggling to complete the task
Want results from more Discord servers?
Add your server