+ 58

What are JS objects used for?

Looking for examples of why, when, how to use var bar = {...}, or an deep explanation as I find it complicated to understand code that have these.

15th May 2018, 7:39 PM
Miguel Melo
Miguel Melo - avatar
38 Antworten
+ 128
An object is just a container. It contains key value pairs. var person = { firstName: "Bob", lastName: "Smith" } person.firstName outputs Bob. Now instead of having to keep track of a ton of individual variables about the person they are all in one convenient place inside a person object. Values inside of an object can also be other objects or even arrays. Or arrays of objects. var student = { name: "Bob Smith", classes: [ { name: "Biology", time: "9:00 am" }, { name: "French Lit", time: "11:30 am" } ] } This is a great way to store information for loops ti access and build html via the DOM. Since JS treats functions as objects you can even do: var datepicker ={ start: "2018-01-01", end: "2018-01-31", change: function(selected) { alert("new date: ", selected); } } This defines a callback function named change. This is something you see on datepicker libraries where you pass in options for it and change let's you define a callback function for when the selected date changes. JSON, JavaScript Object Notation is the preferred way for transferring data when using AJAX calls. Objects are very powerful and have more uses than I could ever post about here.
15th May 2018, 8:09 PM
Adam
Adam - avatar
+ 33
Adam has got this quite covered with answer but this is my addition.look at this:: var man_name="bob" var man_age="23" var man_city="madrid".... Now lets say you go on with this syntax for like a 1000 times,you find out your code is messed up. just use:: var man={age:12,name:"bob",city:"madrid} This way with objects,all properties are easily accessible and your code is much neater
18th May 2018, 3:52 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 29
WHY coz it's a simple yet effective key-value based data structure provided by JavaScript. WHEN whenever you feel like "Oh these stuffs are related let's pack them together 😃" for example, we have var age = 20, height = "5,11" , isCrazy= false; hmm not good since they are all characteristics of a person therefore better to pack it as a person Object var person = { age: 20, height: "5,11", isCrazy: false } HOW by using the example above, or by many other ways like -Object.create() -Object.assign() :- creates a shallow copy for objects using existing objects - using constructor functions and the "new" keyword
18th May 2018, 3:57 PM
Morpheus
Morpheus - avatar
+ 20
Your Friend hi , answer to your question to access French lit in given object var student = { name: "Bob Smith", classes: [ { name: "Biology", time: "9:00 am" }, { name: "French Lit", time: "11:30 am" } ] } student.classes[1].name; or student ["classes"][1].name; or student ["classes"][1]["name"]; or student.classes[1]["name"]; a bit more details on when to use - dot notation - [ ] notation when accessing Object properties https://code.sololearn.com/WT1rPvuw28y2/?ref=app
19th May 2018, 2:51 AM
Morpheus
Morpheus - avatar
+ 17
JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. This chapter describes how to use objects, properties, functions, and methods, and how to create your own objects. 😄😅😍😎 CHECK HERE: 1.)W3 SCHOOLS https://www.w3schools.com/js/js_object_definition.asp 2.) DEVELOPER https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects 3.)https://www.tutorialspoint.com/javascript/javascript_objects.htm 4.)https://javascript.info/object THANKS @ROYAL BB ☺
18th May 2018, 3:20 PM
Brijesh B
Brijesh B - avatar
+ 8
Hello community! I hope this small answer can help. Objects in JavaScript, as in many other programming languages, can be compared with real-life objects. The concept of Objects in JavaScript can be understood as in real life, tangible objects. In JavaScript, an object is an independent entity with properties and types. Compare it with a cup for example. A cup is an object, with properties. A cup has a color, a design, weight, a material from which it was made, etc. In the same way, JavaScript objects can have properties, which define their characteristics.
20th May 2018, 5:27 PM
Norman Raiti Valenzuela Zavala
Norman Raiti Valenzuela Zavala - avatar
+ 7
An excellent series for js objects on youtube: (7 videos spanning from 3 to 7 minutes) https://www.youtube.com/playlist?list=PL4cUxeGkcC9i5yvDkJgt60vNVWffpblB7
18th May 2018, 6:11 PM
Haris
Haris - avatar
+ 7
👍
22nd May 2018, 10:57 AM
FIREmonger
FIREmonger - avatar
+ 5
An object is a container which contains some data that are key,value,pairs, even that can be an object also but both are independent. Each object have some characteristics. Ex:) A car can be red, a building can be small, a bottle can be empty. These characteristics are also called attributes. An attribute describes the current state of an object. Objects can have multiple attributes (the building can be red, empty and small). Each object has unique identity. Ex:). var car{ name: "Audi"; color: "red"; } Now the car object has the following attributes and it performs the methods like move(), run(), drive ().
19th May 2018, 12:35 AM
Sourav Dutta
Sourav Dutta - avatar
+ 5
var myObj = { " yo its a property name😸": "hi", two : { child: " hello"}, func: function(){ alert("hi"); } } /*so we can access child like any of these - myObj.two.child - myObj[ "two"]["child"] - myObj[ "two" ].child - myObj.two[ " child" ] */ console.log(myObj.two.child); console.log(myObj["two"]["child"]); console.log(myObj["two"].child); console.log(myObj.two["child"]); /*we can also access function like these - myObj.func() - myObj[ "func"]() */ myObj.func(); myObj[ "func"](); /*Now the important question when to use [ ] or . ( dot) we need [ ], only in 2 cases that i know for(let key in myObj ){ console.log(key + " : " + myObj[key]); } console.log(myObj[" yo its a property name"]);
19th May 2018, 9:27 AM
oTARANo
oTARANo - avatar
+ 4
Thanks Everybody I also wanted to know that
19th May 2018, 1:38 AM
Cool Coder 😎😎
Cool Coder 😎😎 - avatar
+ 4
Thanks Morpheus!
19th May 2018, 4:00 AM
Your Friend
Your Friend - avatar
+ 3
Think you are living in a room. You are an object in the room. You put a box in the room and you name it Miguel Melo now you put your briefcase inside that box, you put an other box in it, and you label that as studybox. Now your room is a container, it contains you and your box. Both are objects. Your box is also a container that can have more items in it. either they are container or object. So objects are containers to keep data and objects temporarily. In JS, we can put data in containers as we required. there are not too much restrictions on how we can keep our data. Its your own space, utilize as required. fill it with boxes, fill with items or as you like.
19th May 2018, 5:11 AM
Rafique Abdullah
+ 3
var bar is simply referring to its type. A variable by the name var is set to whatever data passed through its parameters. JS is different as var and function are used to define variables and methods, while in other programming the Type and Identifier and method are used. Example Book variable; public void setMethod;
22nd May 2018, 1:19 AM
Apple Blossom
Apple Blossom - avatar
+ 2
Adam, how would you select an object inside of an array inside of an object? In your example, how would you select the value “French Lit”?
19th May 2018, 2:31 AM
Your Friend
Your Friend - avatar
+ 2
Of all JavaScript's objects, the String object is the most commonly used. In the Netscape 2.0 JavaScript implementation, new string objects are created implicitly using a variable assignment. For example, ... creates a string, with the specified text, called myString. All in all Objects are useful for example for making a single unit out of values that belong together.
19th May 2018, 4:52 AM
Andrew Watts
Andrew Watts - avatar
+ 1
You can use them in games, An example is Minecraft, where you have different mosters. You can create a class monster where you need some requires as kind, color, sound, attck points, etc. You use the class monster as a template which later you change to create a specifically monster. I hope be helpful
18th May 2018, 9:35 PM
Christopher Manzano
Christopher Manzano - avatar
+ 1
hcchxh
19th May 2018, 7:05 AM
Billy Marlowe
Billy Marlowe - avatar
+ 1
Ajax!
19th May 2018, 12:35 PM
Angelo
Angelo - avatar
+ 1
Ok
19th May 2018, 12:43 PM
waseem sabbah
waseem sabbah - avatar