parts of an object vs separate objects

Matt Gross

What's the benefit to using an object with some stuff in it, like

var cheese = {color: "bleu", smell: "bad", holes: false};

as opposed to storing separate objects for similar data, like

var cheeseColor = "bleu";
var cheeseSmell = "bad";
var cheeseHoles = false;

I know there's higher-level benefits when it comes to prototyping and constructor functions, but for a simple task, does it make a significant difference?

Bálint

1.) The biggest benefit (in my opinion) is dynamic variable handling.

Let's assume you have an object, but it's loaded through AJAX or the variables aren't made by you. Either way you don't know what's in it.

With normal variables, you're screwed. End.

But with objects, you can ask the user what he wants, and the with the [] operator, you can get that value:

var obj = {
    s: 5,
    c: "abc",
    g: /hello/g
};
function getObjectParam(name) {
    return obj[name];
}

Then by calling

getObjectParam("s")/

You get 5.

This is a very simple use case. I mostly use this feature when I need to store different matrices when I use WebGL for example.

2.) Passing them

Let's say you have a library which needs something configured by the user of the lib. Instead of creating a constructor or a function, which takes in 20 arguments, you can make one which takes in an object, and you use it's values to set everything. This doesn't just make you be able to write a shorter function, but also enables the user to reuse the object if he wants to call the function again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

separate an object into three parts

From Dev

Array of objects vs Object of Objects

From Dev

Array of Objects vs Nested Object?

From Dev

How to separate URL into to parts

From Dev

JavaScript - The Good Parts: Function prototypes vs Object prototypes

From Dev

vector of object all the same object after adding separate objects to it

From Dev

Design: why does JavaScript have separate Function and Object objects, and why do these have their own separate prototype objects?

From Java

State as array of objects vs object keyed by id

From Dev

deleting objects if string of object matches string in a separate array

From Dev

Run Parts of a Pipeline as Separate Job

From Dev

Java - separate number into 3 parts

From Dev

Display multiple parts of an object?

From Dev

OpenCV, divide object into parts

From Dev

How to separate the real and imaginary parts of a transfer function?

From Dev

Division of a convex hull into two separate parts

From Dev

how to separate html list to 4 parts?

From Dev

Separate parts of the GUI with lines in Glade 3

From Dev

How to separate range of integers into two equal parts

From Dev

Split MySQL 'TIME' field into separate parts

From Dev

How to extract various parts of a string into separate strings

From Dev

Separate list into two equal parts with slices

From Java

java.util.Objects.isNull vs object == null

From Dev

java.util.Objects.isNull vs object == null

From Dev

Create New Objects vs Overwrite Existing Object (Performance)

From Dev

Split json into separate objects

From Dev

Separate objects or strings in NSMutableArray?

From Dev

Reuse set of many objects but now assign each object a separate opacity, without copying all code?

From Dev

How to get parts of JSON object

From Dev

How to read parts of a file into an object

Related Related

  1. 1

    separate an object into three parts

  2. 2

    Array of objects vs Object of Objects

  3. 3

    Array of Objects vs Nested Object?

  4. 4

    How to separate URL into to parts

  5. 5

    JavaScript - The Good Parts: Function prototypes vs Object prototypes

  6. 6

    vector of object all the same object after adding separate objects to it

  7. 7

    Design: why does JavaScript have separate Function and Object objects, and why do these have their own separate prototype objects?

  8. 8

    State as array of objects vs object keyed by id

  9. 9

    deleting objects if string of object matches string in a separate array

  10. 10

    Run Parts of a Pipeline as Separate Job

  11. 11

    Java - separate number into 3 parts

  12. 12

    Display multiple parts of an object?

  13. 13

    OpenCV, divide object into parts

  14. 14

    How to separate the real and imaginary parts of a transfer function?

  15. 15

    Division of a convex hull into two separate parts

  16. 16

    how to separate html list to 4 parts?

  17. 17

    Separate parts of the GUI with lines in Glade 3

  18. 18

    How to separate range of integers into two equal parts

  19. 19

    Split MySQL 'TIME' field into separate parts

  20. 20

    How to extract various parts of a string into separate strings

  21. 21

    Separate list into two equal parts with slices

  22. 22

    java.util.Objects.isNull vs object == null

  23. 23

    java.util.Objects.isNull vs object == null

  24. 24

    Create New Objects vs Overwrite Existing Object (Performance)

  25. 25

    Split json into separate objects

  26. 26

    Separate objects or strings in NSMutableArray?

  27. 27

    Reuse set of many objects but now assign each object a separate opacity, without copying all code?

  28. 28

    How to get parts of JSON object

  29. 29

    How to read parts of a file into an object

HotTag

Archive