0

Can anyone give me an example or two of practical user defined objects in JavaScript, i.e. when you need to create an object as opposed to accessing existing predefined objects such as the document object or element objects.

The examples laid out in tutorials to introduce objects, methods and properties are great for the basics but are not typical of what one might need in a script for a web page.

Thanks

groober
  • 113
  • 10
  • 1
    There are no "typical" user defined JS objects. You create an object for what ever purpose you happen to need it. – Teemu Jan 06 '16 at 09:43
  • @Teemu - You are right of course, but in a front-end context where many sites share functionality features, it's reasonable to ask what objects developers are in the habit of making for common tasks – groober Jan 06 '16 at 10:15
  • Data storing is probably the only really common task, any other task can be done with or without objects. There are so many coding styles, functional programmers hardly ever create objects (outside of data storing), some (like me) make everything with objects, styles can be mixed ... Your question really is too broad to answer at SO, – Teemu Jan 06 '16 at 10:46
  • Happy for this to be closed. Apologies if too broad. I had a good search before posting and could not find a satisfactory discussion. Given that so much can be done with built in objects I wanted to know if there were stand out situations where the developer might create their own objects. Thanks – groober Jan 06 '16 at 12:20

1 Answers1

0

Just to give a use-case of usage of a user-defined object, you can build an object (like a bean) like this from the user inputs on a form

var form = {
   name : "",
   age: "",
   email: ""
};

before validating the user input on a form you can populate this object from the user-input on a form. You can then validate the input in this object and if it is valid, submit the same to the server.

By using this object, you didn't have to serialize the data specifically while submitting the form.

This is nowhere near comprehensive/detailed explanation of how user-defined objects can be used, but should be enough to get you started.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94