Javascript - How do I pass a json object in my class constructor?

Asked

Viewed 153 times

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?

  • @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.

  • 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.

  • I understood, I heard that with instanceof I could do something similar, I will look for it.

  • Magno, you would like to pass the entire instance (methods and parameters) of an object into another object and use them there?

1 answer

1

Magno, to pass parameters to the constructor of a class, all you need to do is instantiate it and within the parentheses add the parameter you want to rescue within the class, example:

class Teste {
    constructor(objetoJson) {
      alert(objetoJson.valorA);
    }
}

const meuJson = {
 valorA: 'texto',
 valorB: 20,
 valorC: 'etc',
};

new Teste(meuJson);

Just receive the values from this json within a variable and send to the class.

  • Thanks for the answer, so this is where I knew that in order to pass a json on the instance of a class, I would need the constructor to report something there, but what I really wanted to do was restrict the information that is passed inside that json object, so that the user does not report anything that was not determined by me before, for example you passed in your json o: valora, valorB and valorC, let’s assume that I only allow you to pass the value key and the others will not be allowed, so answer me on this question just to do this with Typescript.

  • You can make a "Whitelist" and then map the json object with the function map or filter of js, and then determine what may or may not be passed to the class. This you can do before, or within the builder himself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.