{}.hasOwnProperty('id'); returns syntax error in console
I played around in the Chrome console and wondered why this statement throws a syntax error:
{}.hasOwnProperty('id');
My expectation was the return value false
.
A syntax error also occurs in Firefox and IE .
The same works if I use an empty array instead of an empty object.
The same also works if I put braces around:
({}.hasOwnProperty('id'));
javascript google-chrome
add a comment |
I played around in the Chrome console and wondered why this statement throws a syntax error:
{}.hasOwnProperty('id');
My expectation was the return value false
.
A syntax error also occurs in Firefox and IE .
The same works if I use an empty array instead of an empty object.
The same also works if I put braces around:
({}.hasOwnProperty('id'));
javascript google-chrome
FYI:({}).hasOwnProperty('id');
works, too.
– Cerbrus
Oct 9 '14 at 11:55
6
{}
without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.
– Frédéric Hamidi
Oct 9 '14 at 11:56
2
In this case,{}
will be interpreted as a block statement rather than an object literal.
– Ben Jackson
Oct 9 '14 at 11:57
Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?
– enguerranws
Oct 9 '14 at 11:58
add a comment |
I played around in the Chrome console and wondered why this statement throws a syntax error:
{}.hasOwnProperty('id');
My expectation was the return value false
.
A syntax error also occurs in Firefox and IE .
The same works if I use an empty array instead of an empty object.
The same also works if I put braces around:
({}.hasOwnProperty('id'));
javascript google-chrome
I played around in the Chrome console and wondered why this statement throws a syntax error:
{}.hasOwnProperty('id');
My expectation was the return value false
.
A syntax error also occurs in Firefox and IE .
The same works if I use an empty array instead of an empty object.
The same also works if I put braces around:
({}.hasOwnProperty('id'));
javascript google-chrome
javascript google-chrome
edited Oct 9 '14 at 11:55
Cerbrus
50k1096117
50k1096117
asked Oct 9 '14 at 11:52
user3745438user3745438
134
134
FYI:({}).hasOwnProperty('id');
works, too.
– Cerbrus
Oct 9 '14 at 11:55
6
{}
without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.
– Frédéric Hamidi
Oct 9 '14 at 11:56
2
In this case,{}
will be interpreted as a block statement rather than an object literal.
– Ben Jackson
Oct 9 '14 at 11:57
Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?
– enguerranws
Oct 9 '14 at 11:58
add a comment |
FYI:({}).hasOwnProperty('id');
works, too.
– Cerbrus
Oct 9 '14 at 11:55
6
{}
without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.
– Frédéric Hamidi
Oct 9 '14 at 11:56
2
In this case,{}
will be interpreted as a block statement rather than an object literal.
– Ben Jackson
Oct 9 '14 at 11:57
Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?
– enguerranws
Oct 9 '14 at 11:58
FYI:
({}).hasOwnProperty('id');
works, too.– Cerbrus
Oct 9 '14 at 11:55
FYI:
({}).hasOwnProperty('id');
works, too.– Cerbrus
Oct 9 '14 at 11:55
6
6
{}
without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.– Frédéric Hamidi
Oct 9 '14 at 11:56
{}
without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.– Frédéric Hamidi
Oct 9 '14 at 11:56
2
2
In this case,
{}
will be interpreted as a block statement rather than an object literal.– Ben Jackson
Oct 9 '14 at 11:57
In this case,
{}
will be interpreted as a block statement rather than an object literal.– Ben Jackson
Oct 9 '14 at 11:57
Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?
– enguerranws
Oct 9 '14 at 11:58
Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?
– enguerranws
Oct 9 '14 at 11:58
add a comment |
2 Answers
2
active
oldest
votes
There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:
if (x) {
...
}
and they are used to declare object literals:
var obj = {
prop: "value"
}
Constext is used to differentiate between the two interpretations, and in case of:
{}.hasOwnProperty('id');
braces are resolved to block declaration, so this yields syntax error. On the other hand:
({}.hasOwnProperty('id'));
One cannot declare block inside parentheses, so {}
is recognized as object literal in this context.
This is a complete answer IMHO.
– enguerranws
Oct 9 '14 at 12:04
add a comment |
{} is treated as a statement block rather than an object, as you are expecting. Hence the error.
EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.
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%2f26277767%2fhasownpropertyid-returns-syntax-error-in-console%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
There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:
if (x) {
...
}
and they are used to declare object literals:
var obj = {
prop: "value"
}
Constext is used to differentiate between the two interpretations, and in case of:
{}.hasOwnProperty('id');
braces are resolved to block declaration, so this yields syntax error. On the other hand:
({}.hasOwnProperty('id'));
One cannot declare block inside parentheses, so {}
is recognized as object literal in this context.
This is a complete answer IMHO.
– enguerranws
Oct 9 '14 at 12:04
add a comment |
There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:
if (x) {
...
}
and they are used to declare object literals:
var obj = {
prop: "value"
}
Constext is used to differentiate between the two interpretations, and in case of:
{}.hasOwnProperty('id');
braces are resolved to block declaration, so this yields syntax error. On the other hand:
({}.hasOwnProperty('id'));
One cannot declare block inside parentheses, so {}
is recognized as object literal in this context.
This is a complete answer IMHO.
– enguerranws
Oct 9 '14 at 12:04
add a comment |
There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:
if (x) {
...
}
and they are used to declare object literals:
var obj = {
prop: "value"
}
Constext is used to differentiate between the two interpretations, and in case of:
{}.hasOwnProperty('id');
braces are resolved to block declaration, so this yields syntax error. On the other hand:
({}.hasOwnProperty('id'));
One cannot declare block inside parentheses, so {}
is recognized as object literal in this context.
There's syntax ambiguity in snippet in question. Curly braces in JavaScript have two meanings: they are used to delimit code blocks, for example:
if (x) {
...
}
and they are used to declare object literals:
var obj = {
prop: "value"
}
Constext is used to differentiate between the two interpretations, and in case of:
{}.hasOwnProperty('id');
braces are resolved to block declaration, so this yields syntax error. On the other hand:
({}.hasOwnProperty('id'));
One cannot declare block inside parentheses, so {}
is recognized as object literal in this context.
edited Oct 9 '14 at 12:02
enguerranws
5,57543472
5,57543472
answered Oct 9 '14 at 12:00
el.pescadoel.pescado
16k23073
16k23073
This is a complete answer IMHO.
– enguerranws
Oct 9 '14 at 12:04
add a comment |
This is a complete answer IMHO.
– enguerranws
Oct 9 '14 at 12:04
This is a complete answer IMHO.
– enguerranws
Oct 9 '14 at 12:04
This is a complete answer IMHO.
– enguerranws
Oct 9 '14 at 12:04
add a comment |
{} is treated as a statement block rather than an object, as you are expecting. Hence the error.
EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.
add a comment |
{} is treated as a statement block rather than an object, as you are expecting. Hence the error.
EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.
add a comment |
{} is treated as a statement block rather than an object, as you are expecting. Hence the error.
EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.
{} is treated as a statement block rather than an object, as you are expecting. Hence the error.
EDIT: As @Cerbrus mentioned in the comments, ({}).hasOwnProperty('id'); would return false and not an error. This is because when surrounding {} with parenthesis, it is interpreted as an Object.
answered Oct 9 '14 at 12:00
gopi1410gopi1410
4,15073368
4,15073368
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%2f26277767%2fhasownpropertyid-returns-syntax-error-in-console%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
FYI:
({}).hasOwnProperty('id');
works, too.– Cerbrus
Oct 9 '14 at 11:55
6
{}
without surrounding parentheses (or an operator applied) is parsed as a statement block, not an object literal.– Frédéric Hamidi
Oct 9 '14 at 11:56
2
In this case,
{}
will be interpreted as a block statement rather than an object literal.– Ben Jackson
Oct 9 '14 at 11:57
Object.hasOwnProperty('id'); return false. I think something about naming convention could explain that ?
– enguerranws
Oct 9 '14 at 11:58