Search code examples
javascriptobjectprompt

How do I Create Objects with a Prompt?


The question says it all. Is it even possible for the user to create new objects by responding to a prompt? And if it is possible, then could I also add properties to those objects? (Instructions on this, too, please.

The reason why I'm asking this is because I'm currently doing a school project involving a small database showing our understanding of objects, arrays, and such. I'm not very experienced in Javascript, so a somewhat straightforward answer would be great.

I've noticed a lot of the responses from people on this site include parts of Javascript I don't understand / haven't learned yet.

Please let me know if I can clarify anything.


Solution

  • You do it like this:

    var answer = prompt('...')
    # now create your object however you want
    var obj = new MyObject(answer)
    

    Prompt itself can't create any objects, of course. It has only one job and does it well.