I am having problem understanding the behavior of below code in JavaScript Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Convert character to ASCII code in JavaScriptInvoking JavaScript code in an iframe from the parent pageHow to execute a JavaScript function when I have its name as a stringHow to find event listeners on a DOM node when debugging or from the JavaScript code?Simplest code for array intersection in javascriptDoes JavaScript have a method like “range()” to generate a range within the supplied bounds?How to set a JavaScript breakpoint from code in Chrome?Can't access object property, even though it exists. Returns undefinedDoes Javascript writable descriptor prevent changes on instances?Can't define set and get methods
Is it a good idea to use CNN to classify 1D signal?
What would you call this weird metallic apparatus that allows you to lift people?
What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?
Denied boarding although I have proper visa and documentation. To whom should I make a complaint?
Is it fair for a professor to grade us on the possession of past papers?
What initially awakened the Balrog?
Chinese Seal on silk painting - what does it mean?
Why should I vote and accept answers?
Project Euler #1 in C++
Why is it faster to reheat something than it is to cook it?
How would a mousetrap for use in space work?
How fail-safe is nr as stop bytes?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
Is there hard evidence that the grant peer review system performs significantly better than random?
Do wooden building fires get hotter than 600°C?
Take 2! Is this homebrew Lady of Pain warlock patron balanced?
Can the Great Weapon Master feat's damage bonus and accuracy penalty apply to attacks from the Spiritual Weapon spell?
Why weren't discrete x86 CPUs ever used in game hardware?
How to play a character with a disability or mental disorder without being offensive?
I am having problem understanding the behavior of below code in JavaScript
How can I reduce the gap between left and right of cdot with a macro?
How to react to hostile behavior from a senior developer?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
How do I find out the mythology and history of my Fortress?
I am having problem understanding the behavior of below code in JavaScript
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Convert character to ASCII code in JavaScriptInvoking JavaScript code in an iframe from the parent pageHow to execute a JavaScript function when I have its name as a stringHow to find event listeners on a DOM node when debugging or from the JavaScript code?Simplest code for array intersection in javascriptDoes JavaScript have a method like “range()” to generate a range within the supplied bounds?How to set a JavaScript breakpoint from code in Chrome?Can't access object property, even though it exists. Returns undefinedDoes Javascript writable descriptor prevent changes on instances?Can't define set and get methods
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I was playing with below javascript code. Understanding of Object.defineProperty()
and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct
When I debug the code and evaluate the profile I can see the name & age
property in the object
But at the time of output, it only shows the name
property
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true
)
console.log(profile)
console.log(profile.age)
Now expected output here should be
name: "Barry Allen", age: 23
23
but I get the output as.
Note that I am able to access the age
property defined afterwards.
I am not sure why the console.log()
is behaving this way.
name: "Barry Allen"
23
javascript
New contributor
add a comment |
I was playing with below javascript code. Understanding of Object.defineProperty()
and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct
When I debug the code and evaluate the profile I can see the name & age
property in the object
But at the time of output, it only shows the name
property
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true
)
console.log(profile)
console.log(profile.age)
Now expected output here should be
name: "Barry Allen", age: 23
23
but I get the output as.
Note that I am able to access the age
property defined afterwards.
I am not sure why the console.log()
is behaving this way.
name: "Barry Allen"
23
javascript
New contributor
add a comment |
I was playing with below javascript code. Understanding of Object.defineProperty()
and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct
When I debug the code and evaluate the profile I can see the name & age
property in the object
But at the time of output, it only shows the name
property
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true
)
console.log(profile)
console.log(profile.age)
Now expected output here should be
name: "Barry Allen", age: 23
23
but I get the output as.
Note that I am able to access the age
property defined afterwards.
I am not sure why the console.log()
is behaving this way.
name: "Barry Allen"
23
javascript
New contributor
I was playing with below javascript code. Understanding of Object.defineProperty()
and I am facing a strange issue with it. When I try to execute below code in the browser or in the VS code the output is not as expected whereas if I try to debug the code the output is correct
When I debug the code and evaluate the profile I can see the name & age
property in the object
But at the time of output, it only shows the name
property
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true
)
console.log(profile)
console.log(profile.age)
Now expected output here should be
name: "Barry Allen", age: 23
23
but I get the output as.
Note that I am able to access the age
property defined afterwards.
I am not sure why the console.log()
is behaving this way.
name: "Barry Allen"
23
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true
)
console.log(profile)
console.log(profile.age)
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true
)
console.log(profile)
console.log(profile.age)
javascript
javascript
New contributor
New contributor
edited 47 mins ago
Eddie
20.3k51642
20.3k51642
New contributor
asked 48 mins ago
Ravi WRavi W
384
384
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You should set enumerable
to true
. In Object.defineProperty
its false
by default. According to MDN.
enumerable
true
if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Non-enumerable means that property will not be shown in Object.keys()
or for..in
loop neither in console
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
All the properties and methods on prototype
object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.
To get all properties(including non-enumerable)Object.getOwnPropertyNames()
.
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: false
)
for(let key in profile) console.log(key) //only name will be displayed.
console.log(Object.getOwnPropertyNames(profile)) //You will se age too
I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifyingenumerable
to false).
– randomSoul
35 mins ago
@randomSoul I can't get what you mean.
– Maheer Ali
30 mins ago
See - pasteboard.co/IaOxMqB.png . I did not setenumerable
to true forage
, but still it is shown.
– randomSoul
24 mins ago
@randomSoul See the comments of the answer of certain performance.
– Maheer Ali
5 mins ago
1
@randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.
– Yong Quan
3 mins ago
add a comment |
By default, properties you define with defineProperty
are not enumerable - this means that they will not show up when you iterate over their Object.keys
(which is what the snippet console does). (Similarly, the length
property of an array does not get displayed, because it's non-enumerable.)
See MDN:
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Make it enumerable instead:
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:
See how age
is grey-ish, while name
is not - this indicates that name
is enumerable, and age
is not.
Someone give this pasteboard.co/IaOxMqB.png Its showingage
property in chrome console. Can you please explain that? Does chrome console works differently?
– Maheer Ali
12 mins ago
Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (likeage
and__proto__
) will be slightly greyed out.
– CertainPerformance
6 mins ago
add a comment |
Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable : true,
configurable : true
)
console.log(profile)
console.log(profile.age)
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
);
);
Ravi W is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55757089%2fi-am-having-problem-understanding-the-behavior-of-below-code-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should set enumerable
to true
. In Object.defineProperty
its false
by default. According to MDN.
enumerable
true
if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Non-enumerable means that property will not be shown in Object.keys()
or for..in
loop neither in console
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
All the properties and methods on prototype
object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.
To get all properties(including non-enumerable)Object.getOwnPropertyNames()
.
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: false
)
for(let key in profile) console.log(key) //only name will be displayed.
console.log(Object.getOwnPropertyNames(profile)) //You will se age too
I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifyingenumerable
to false).
– randomSoul
35 mins ago
@randomSoul I can't get what you mean.
– Maheer Ali
30 mins ago
See - pasteboard.co/IaOxMqB.png . I did not setenumerable
to true forage
, but still it is shown.
– randomSoul
24 mins ago
@randomSoul See the comments of the answer of certain performance.
– Maheer Ali
5 mins ago
1
@randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.
– Yong Quan
3 mins ago
add a comment |
You should set enumerable
to true
. In Object.defineProperty
its false
by default. According to MDN.
enumerable
true
if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Non-enumerable means that property will not be shown in Object.keys()
or for..in
loop neither in console
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
All the properties and methods on prototype
object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.
To get all properties(including non-enumerable)Object.getOwnPropertyNames()
.
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: false
)
for(let key in profile) console.log(key) //only name will be displayed.
console.log(Object.getOwnPropertyNames(profile)) //You will se age too
I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifyingenumerable
to false).
– randomSoul
35 mins ago
@randomSoul I can't get what you mean.
– Maheer Ali
30 mins ago
See - pasteboard.co/IaOxMqB.png . I did not setenumerable
to true forage
, but still it is shown.
– randomSoul
24 mins ago
@randomSoul See the comments of the answer of certain performance.
– Maheer Ali
5 mins ago
1
@randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.
– Yong Quan
3 mins ago
add a comment |
You should set enumerable
to true
. In Object.defineProperty
its false
by default. According to MDN.
enumerable
true
if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Non-enumerable means that property will not be shown in Object.keys()
or for..in
loop neither in console
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
All the properties and methods on prototype
object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.
To get all properties(including non-enumerable)Object.getOwnPropertyNames()
.
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: false
)
for(let key in profile) console.log(key) //only name will be displayed.
console.log(Object.getOwnPropertyNames(profile)) //You will se age too
You should set enumerable
to true
. In Object.defineProperty
its false
by default. According to MDN.
enumerable
true
if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Non-enumerable means that property will not be shown in Object.keys()
or for..in
loop neither in console
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
All the properties and methods on prototype
object of built-in classes are non-enumerable. Thats is the reason you can call them from instance but they don't appear while iterating.
To get all properties(including non-enumerable)Object.getOwnPropertyNames()
.
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: false
)
for(let key in profile) console.log(key) //only name will be displayed.
console.log(Object.getOwnPropertyNames(profile)) //You will se age too
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: false
)
for(let key in profile) console.log(key) //only name will be displayed.
console.log(Object.getOwnPropertyNames(profile)) //You will se age too
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile , 'age',
value: 23,
writable: true,
enumerable: false
)
for(let key in profile) console.log(key) //only name will be displayed.
console.log(Object.getOwnPropertyNames(profile)) //You will se age too
edited 1 min ago
answered 46 mins ago
Maheer AliMaheer Ali
11.5k826
11.5k826
I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifyingenumerable
to false).
– randomSoul
35 mins ago
@randomSoul I can't get what you mean.
– Maheer Ali
30 mins ago
See - pasteboard.co/IaOxMqB.png . I did not setenumerable
to true forage
, but still it is shown.
– randomSoul
24 mins ago
@randomSoul See the comments of the answer of certain performance.
– Maheer Ali
5 mins ago
1
@randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.
– Yong Quan
3 mins ago
add a comment |
I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifyingenumerable
to false).
– randomSoul
35 mins ago
@randomSoul I can't get what you mean.
– Maheer Ali
30 mins ago
See - pasteboard.co/IaOxMqB.png . I did not setenumerable
to true forage
, but still it is shown.
– randomSoul
24 mins ago
@randomSoul See the comments of the answer of certain performance.
– Maheer Ali
5 mins ago
1
@randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.
– Yong Quan
3 mins ago
I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying
enumerable
to false).– randomSoul
35 mins ago
I didn't knew about this, but when I checked by running the local code in browser, it shows up perfectly (in spite of explicitly specifying
enumerable
to false).– randomSoul
35 mins ago
@randomSoul I can't get what you mean.
– Maheer Ali
30 mins ago
@randomSoul I can't get what you mean.
– Maheer Ali
30 mins ago
See - pasteboard.co/IaOxMqB.png . I did not set
enumerable
to true for age
, but still it is shown.– randomSoul
24 mins ago
See - pasteboard.co/IaOxMqB.png . I did not set
enumerable
to true for age
, but still it is shown.– randomSoul
24 mins ago
@randomSoul See the comments of the answer of certain performance.
– Maheer Ali
5 mins ago
@randomSoul See the comments of the answer of certain performance.
– Maheer Ali
5 mins ago
1
1
@randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.
– Yong Quan
3 mins ago
@randomSoul In Chrome console, you should see unenumerable properties colored a little bit transparent.
– Yong Quan
3 mins ago
add a comment |
By default, properties you define with defineProperty
are not enumerable - this means that they will not show up when you iterate over their Object.keys
(which is what the snippet console does). (Similarly, the length
property of an array does not get displayed, because it's non-enumerable.)
See MDN:
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Make it enumerable instead:
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:
See how age
is grey-ish, while name
is not - this indicates that name
is enumerable, and age
is not.
Someone give this pasteboard.co/IaOxMqB.png Its showingage
property in chrome console. Can you please explain that? Does chrome console works differently?
– Maheer Ali
12 mins ago
Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (likeage
and__proto__
) will be slightly greyed out.
– CertainPerformance
6 mins ago
add a comment |
By default, properties you define with defineProperty
are not enumerable - this means that they will not show up when you iterate over their Object.keys
(which is what the snippet console does). (Similarly, the length
property of an array does not get displayed, because it's non-enumerable.)
See MDN:
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Make it enumerable instead:
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:
See how age
is grey-ish, while name
is not - this indicates that name
is enumerable, and age
is not.
Someone give this pasteboard.co/IaOxMqB.png Its showingage
property in chrome console. Can you please explain that? Does chrome console works differently?
– Maheer Ali
12 mins ago
Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (likeage
and__proto__
) will be slightly greyed out.
– CertainPerformance
6 mins ago
add a comment |
By default, properties you define with defineProperty
are not enumerable - this means that they will not show up when you iterate over their Object.keys
(which is what the snippet console does). (Similarly, the length
property of an array does not get displayed, because it's non-enumerable.)
See MDN:
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Make it enumerable instead:
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:
See how age
is grey-ish, while name
is not - this indicates that name
is enumerable, and age
is not.
By default, properties you define with defineProperty
are not enumerable - this means that they will not show up when you iterate over their Object.keys
(which is what the snippet console does). (Similarly, the length
property of an array does not get displayed, because it's non-enumerable.)
See MDN:
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
Make it enumerable instead:
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
The reason you can see the property in the logged image is that Chrome's console will show you non-enumerable properties as well - but the non-enumerable properties will be slightly greyed-out:
See how age
is grey-ish, while name
is not - this indicates that name
is enumerable, and age
is not.
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable: true
)
console.log(profile)
console.log(profile.age)
edited 7 mins ago
answered 45 mins ago
CertainPerformanceCertainPerformance
101k166291
101k166291
Someone give this pasteboard.co/IaOxMqB.png Its showingage
property in chrome console. Can you please explain that? Does chrome console works differently?
– Maheer Ali
12 mins ago
Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (likeage
and__proto__
) will be slightly greyed out.
– CertainPerformance
6 mins ago
add a comment |
Someone give this pasteboard.co/IaOxMqB.png Its showingage
property in chrome console. Can you please explain that? Does chrome console works differently?
– Maheer Ali
12 mins ago
Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (likeage
and__proto__
) will be slightly greyed out.
– CertainPerformance
6 mins ago
Someone give this pasteboard.co/IaOxMqB.png Its showing
age
property in chrome console. Can you please explain that? Does chrome console works differently?– Maheer Ali
12 mins ago
Someone give this pasteboard.co/IaOxMqB.png Its showing
age
property in chrome console. Can you please explain that? Does chrome console works differently?– Maheer Ali
12 mins ago
Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like
age
and __proto__
) will be slightly greyed out.– CertainPerformance
6 mins ago
Yes, that's a Chrome console behavior - it'll show you all properties, including non-enumerable ones, see edit. The non-enumerable properties (like
age
and __proto__
) will be slightly greyed out.– CertainPerformance
6 mins ago
add a comment |
Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable : true,
configurable : true
)
console.log(profile)
console.log(profile.age)
add a comment |
Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable : true,
configurable : true
)
console.log(profile)
console.log(profile.age)
add a comment |
Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable : true,
configurable : true
)
console.log(profile)
console.log(profile.age)
Whenever you use".defineProperty" method of object. You should better define all the properties of the descriptor. Because if you don't define other property descriptor then it assumes default values for all of them which is false. So your console.log checks for all the enumerable : true properties and logs them.
//Code Snippet
let profile =
name: 'Barry Allen',
// I added a new property in the profile object.
Object.defineProperty(profile, 'age',
value: 23,
writable: true,
enumerable : true,
configurable : true
)
console.log(profile)
console.log(profile.age)
answered 42 mins ago
RK_15RK_15
5749
5749
add a comment |
add a comment |
Ravi W is a new contributor. Be nice, and check out our Code of Conduct.
Ravi W is a new contributor. Be nice, and check out our Code of Conduct.
Ravi W is a new contributor. Be nice, and check out our Code of Conduct.
Ravi W is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55757089%2fi-am-having-problem-understanding-the-behavior-of-below-code-in-javascript%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