0
Good afternoon, I have a question regarding parameters where you pass an object, for example in mongoose
has a method connect
which we passed to url
connection + a objeto
to define the options, and that’s what I’m a little bit in doubt about, how to create this:
How can I do the same in a class? passing the construtor
these options to which the user will feed this object with settings made by me for example.
class Teste {
constructor(arg1, options = {}) {
}
}
For what I tested if I do so options = {}
i can pass an object to the constructor, but the problem is that I can pass anything, if I want to pass 1 thousand keys and values I can, I wanted it to be the same as the example I gave mongoose
when entering the parameter of options
, If I press in Vscode Control + Space I will be able to see all the options I can define, I don’t know if I get your light in what I want, in a summarized way as I can to receive in the constructor an object with properties defined by me?
You want to receive an object in the constructor, but it needs to be a defined object, with the properties you expect, typed?
– Daniel Mendes
@Danielmendes good I am beginner in Javascript, but only you have spoken typed mean that only have to do with Typescript? : / I wanted exactly what you are talking about, but if possible only with Javascript, I know that Typescript is not another language but a superset but, wanted to stay at the moment only with Javascript.
– Magno
Yes, as far as I know, in JS you will not get this result, you can go to TS or receive the object and perform the validations you want, looking for the properties etc.
– Daniel Mendes
I understood, I heard that with instanceof I could do something similar, I will look for it.
– Magno
Magno, you would like to pass the entire instance (methods and parameters) of an object into another object and use them there?
– Glenys Mitchell