Differences in objects?
I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?
Option 1
body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}
Option 2
body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}
body.item1 = '';
body['item2'] = '';
javascript typescript
add a comment |
I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?
Option 1
body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}
Option 2
body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}
body.item1 = '';
body['item2'] = '';
javascript typescript
The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.
– SPlatten
Nov 23 '18 at 10:09
Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?
– Roope
Nov 23 '18 at 10:09
add a comment |
I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?
Option 1
body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}
Option 2
body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}
body.item1 = '';
body['item2'] = '';
javascript typescript
I've seen two ways to use objects, I would like to know what's the difference or it's just different syntax?
Option 1
body(data) = {
item1: data.val1;
item2: data.val2;
item3: data.val3;
}
Option 2
body(data) = {
item1 = data.val1,
item2 = data.val2,
item3 = data.val3
}
body.item1 = '';
body['item2'] = '';
javascript typescript
javascript typescript
asked Nov 23 '18 at 10:02
DangurDangur
646
646
The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.
– SPlatten
Nov 23 '18 at 10:09
Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?
– Roope
Nov 23 '18 at 10:09
add a comment |
The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.
– SPlatten
Nov 23 '18 at 10:09
Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?
– Roope
Nov 23 '18 at 10:09
The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.
– SPlatten
Nov 23 '18 at 10:09
The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.
– SPlatten
Nov 23 '18 at 10:09
Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?
– Roope
Nov 23 '18 at 10:09
Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?
– Roope
Nov 23 '18 at 10:09
add a comment |
2 Answers
2
active
oldest
votes
In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:
const foo={}
const prop= 'item4';
foo[prop] = 'something good'
alert(foo.item4)//Should alert "something good"
add a comment |
There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444480%2fdifferences-in-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:
const foo={}
const prop= 'item4';
foo[prop] = 'something good'
alert(foo.item4)//Should alert "something good"
add a comment |
In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:
const foo={}
const prop= 'item4';
foo[prop] = 'something good'
alert(foo.item4)//Should alert "something good"
add a comment |
In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:
const foo={}
const prop= 'item4';
foo[prop] = 'something good'
alert(foo.item4)//Should alert "something good"
In your example there is no difference, but the array-like syntax will help you once you need to use a variable as the object property, let's say:
const foo={}
const prop= 'item4';
foo[prop] = 'something good'
alert(foo.item4)//Should alert "something good"
answered Nov 23 '18 at 10:08
sheff2k1sheff2k1
444416
444416
add a comment |
add a comment |
There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).
add a comment |
There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).
add a comment |
There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).
There is no difference, you can access properties on Objects via the dot notation or the square brackets notation. To be compliant to the coding standards (and on the why the brackets notation is useful) you should always use the dot notation to reference to properties; the only case in which you should be using the square brackets notation is when you want to reference to a property that is not hard coded but will be referenced at run time. (I.E. when you use a variable to know which property to get).
answered Nov 23 '18 at 10:07
Alpe89Alpe89
18319
18319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444480%2fdifferences-in-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The first example is creating a JSON object, although it should have ',' NOT ';' after each member. The second isn't valid either.
– SPlatten
Nov 23 '18 at 10:09
Are you talking about defining the object or accessing the object? Your option 1 is not valid JS, which you can see by running it anywhere. Neither is the second one though. I have to wonder, where exactly have you seen these used?
– Roope
Nov 23 '18 at 10:09