Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #36870 > unrolled thread

Object prop missing?

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2019-06-13 16:25 +1000
Last post2019-06-13 15:41 +0700
Articles 10 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Object prop missing? Andrew Poulos <ap_prog@hotmail.com> - 2019-06-13 16:25 +1000
    Re: Object prop missing? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-13 09:37 +0200
      Re: Object prop missing? Andrew Poulos <ap_prog@hotmail.com> - 2019-06-13 20:05 +1000
        Re: Object prop missing? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-06-13 12:33 +0200
          Re: Object prop missing? Andrew Poulos <ap_prog@hotmail.com> - 2019-06-13 20:53 +1000
            Re: Object prop missing? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-13 13:09 +0200
              Re: Object prop missing? Andrew Poulos <ap_prog@hotmail.com> - 2019-06-13 21:54 +1000
                Re: Object prop missing? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-13 17:30 +0200
        Re: Object prop missing? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-06-13 13:05 +0200
    Re: Object prop missing? JJ <jj4public@vfemail.net> - 2019-06-13 15:41 +0700

#36870 — Object prop missing?

FromAndrew Poulos <ap_prog@hotmail.com>
Date2019-06-13 16:25 +1000
SubjectObject prop missing?
Message-ID<p6KdnbVLAPPqcZzAnZ2dnUU7-QfNnZ2d@westnet.com.au>
I've created a property and the console it tells me that it looks like this

Object
    elem: div#opt1.opt
    h: 28
    id: "opt1"
    maxw: 548
    on: false
    pos: {x: 46}
    txt: p
    __proto__: Object

yet when, right after, I try to get the value of op.h I get undefined

console.log(op)   //--> give the object above
console.log(op.h) //--> undefined

I don't understand how this could be happening.

Andrew Poulos

[toc] | [next] | [standalone]


#36874

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2019-06-13 09:37 +0200
Message-ID<12600445.cR47uQ6sgG@PointedEars.de>
In reply to#36870
Andrew Poulos wrote:

> I've created a property and the console it tells me that it looks like
> this
> 
> Object
>     elem: div#opt1.opt
>     h: 28
>     id: "opt1"
>     maxw: 548
>     on: false
>     pos: {x: 46}
>     txt: p
>     __proto__: Object
> 
> yet when, right after, I try to get the value of op.h I get undefined
> 
> console.log(op)   //--> give the object above
> console.log(op.h) //--> undefined
> 
> I don't understand how this could be happening.

Show the code used to create and query the object, i.e. the entire relevant 
part of the console transcript, *verbatim*.  And name the runtime 
environment so that one can take at least a guess at the used ECMAScript 
implementation.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#36882

FromAndrew Poulos <ap_prog@hotmail.com>
Date2019-06-13 20:05 +1000
Message-ID<kcidnW32l-lhgp_AnZ2dnUU7-TvNnZ2d@westnet.com.au>
In reply to#36874
On 13/06/2019 5:36 pm, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
> 
>> I've created a property and the console it tells me that it looks like
>> this
>>
>> Object
>>      elem: div#opt1.opt
>>      h: 28
>>      id: "opt1"
>>      maxw: 548
>>      on: false
>>      pos: {x: 46}
>>      txt: p
>>      __proto__: Object
>>
>> yet when, right after, I try to get the value of op.h I get undefined
>>
>> console.log(op)   //--> give the object above
>> console.log(op.h) //--> undefined
>>
>> I don't understand how this could be happening.
> 
> Show the code used to create and query the object, i.e. the entire relevant
> part of the console transcript, *verbatim*.  And name the runtime
> environment so that one can take at least a guess at the used ECMAScript
> implementation.

var options = [
                {},
                {"id": "opt1", "on": false },
                {"id": "opt2", "on": false },
                {"id": "opt3", "on": false },
                {"id": "opt4", "on": false },
                {"id": "opt5", "on": false }
               ];

for (var i = 6; i > 0; i--) {
   // add stuff to options elements
   options[i].h = 28;
}

showOptions();

function showOptions() {
   console.log(options[1]);        // 1.
   console.log(options[1].h)       // 2.
   console.log(typeof options[1]); // 3.

   // code that relies on .h values
}

In Windows Chrome 75.0.3770.80 (64-bit):

1.
 > Object

when opened

v Object
   > elem: div#opt1.opt
     h: 28
     id: "opt1"
     maxw: 548
     on: false
   > pos: {x: 46}
   > txt: p
     __proto__: Object

2. undefined

3. object

h is in options[1] but when I try to get the value it's not.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#36889

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2019-06-13 12:33 +0200
Message-ID<XnsAA6D7FBCFA34eejj99@194.109.6.166>
In reply to#36882
Andrew Poulos <ap_prog@hotmail.com> wrote on 13 Jun 2019 in
comp.lang.javascript: 

> On 13/06/2019 5:36 pm, Thomas 'PointedEars' Lahn wrote:
>> Andrew Poulos wrote:
>> 
>>> I've created a property and the console it tells me that it looks like
>>> this
>>>
>>> Object
>>>      elem: div#opt1.opt
>>>      h: 28
>>>      id: "opt1"
>>>      maxw: 548
>>>      on: false
>>>      pos: {x: 46}
>>>      txt: p
>>>      __proto__: Object
>>>
>>> yet when, right after, I try to get the value of op.h I get undefined
>>>
>>> console.log(op)   //--> give the object above
>>> console.log(op.h) //--> undefined
>>>
>>> I don't understand how this could be happening.
>> 
>> Show the code used to create and query the object, i.e. the entire
>> relevant part of the console transcript, *verbatim*.  And name the
>> runtime environment so that one can take at least a guess at the used
>> ECMAScript implementation.
> 
> var options = [
>                 {},
>                 {"id": "opt1", "on": false },
>                 {"id": "opt2", "on": false },
>                 {"id": "opt3", "on": false },
>                 {"id": "opt4", "on": false },
>                 {"id": "opt5", "on": false }
>                ];
> 
> for (var i = 6; i > 0; i--) {
>    // add stuff to options elements
>    options[i].h = 28;
>}
> 
> showOptions();
> 
> function showOptions() {
>    console.log(options[1]);        // 1.
>    console.log(options[1].h)       // 2.
>    console.log(typeof options[1]); // 3.
> 
>    // code that relies on .h values
>}
> 
> In Windows Chrome 75.0.3770.80 (64-bit):
> 
> 1.
> > Object
> 
> when opened
> 
> v Object
>    > elem: div#opt1.opt
>      h: 28
>      id: "opt1"
>      maxw: 548
>      on: false
>    > pos: {x: 46}
>    > txt: p
>      __proto__: Object
> 
> 2. undefined
> 
> 3. object
> 
> h is in options[1] but when I try to get the value it's not.
> 
> Andrew Poulos

You did not test this, because option[6] does not exist and gives an error.

However this works fine:

var options = [
                {},
                {"id": "opt1", "on": false },
                {"id": "opt2", "on": false },
                {"id": "opt3", "on": false },
                {"id": "opt4", "on": false },
                {"id": "opt5", "on": false }
               ];

options[1].h = 28;

showOptions();

function showOptions() {
   console.log(options[1]); 
   console.log(options[1].h);
   console.log(typeof options[1]);
}

// {id: "opt1", on: false, h: 28}
// 28
// object




-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#36892

FromAndrew Poulos <ap_prog@hotmail.com>
Date2019-06-13 20:53 +1000
Message-ID<4KGdnc1BE6S_tp_AnZ2dnUU7-YXNnZ2d@westnet.com.au>
In reply to#36889
On 13/06/2019 8:33 pm, Evertjan. wrote:
> Andrew Poulos <ap_prog@hotmail.com> wrote on 13 Jun 2019 in
> comp.lang.javascript:
> 
>> On 13/06/2019 5:36 pm, Thomas 'PointedEars' Lahn wrote:
>>> Andrew Poulos wrote:
>>>
>>>> I've created a property and the console it tells me that it looks like
>>>> this
>>>>
>>>> Object
>>>>       elem: div#opt1.opt
>>>>       h: 28
>>>>       id: "opt1"
>>>>       maxw: 548
>>>>       on: false
>>>>       pos: {x: 46}
>>>>       txt: p
>>>>       __proto__: Object
>>>>
>>>> yet when, right after, I try to get the value of op.h I get undefined
>>>>
>>>> console.log(op)   //--> give the object above
>>>> console.log(op.h) //--> undefined
>>>>
>>>> I don't understand how this could be happening.
>>>
>>> Show the code used to create and query the object, i.e. the entire
>>> relevant part of the console transcript, *verbatim*.  And name the
>>> runtime environment so that one can take at least a guess at the used
>>> ECMAScript implementation.
>>
>> var options = [
>>                  {},
>>                  {"id": "opt1", "on": false },
>>                  {"id": "opt2", "on": false },
>>                  {"id": "opt3", "on": false },
>>                  {"id": "opt4", "on": false },
>>                  {"id": "opt5", "on": false }
>>                 ];
>>
>> for (var i = 6; i > 0; i--) {
>>     // add stuff to options elements
>>     options[i].h = 28;
>> }
>>
>> showOptions();
>>
>> function showOptions() {
>>     console.log(options[1]);        // 1.
>>     console.log(options[1].h)       // 2.
>>     console.log(typeof options[1]); // 3.
>>
>>     // code that relies on .h values
>> }
>>
>> In Windows Chrome 75.0.3770.80 (64-bit):
>>
>> 1.
>>> Object
>>
>> when opened
>>
>> v Object
>>     > elem: div#opt1.opt
>>       h: 28
>>       id: "opt1"
>>       maxw: 548
>>       on: false
>>     > pos: {x: 46}
>>     > txt: p
>>       __proto__: Object
>>
>> 2. undefined
>>
>> 3. object
>>
>> h is in options[1] but when I try to get the value it's not.
>>
>> Andrew Poulos
> 
> You did not test this, because option[6] does not exist and gives an error.

Yes, you're right the 6 should've been a 5.

> However this works fine:
> 
> var options = [
>                  {},
>                  {"id": "opt1", "on": false },
>                  {"id": "opt2", "on": false },
>                  {"id": "opt3", "on": false },
>                  {"id": "opt4", "on": false },
>                  {"id": "opt5", "on": false }
>                 ];
> 
> options[1].h = 28;
> 
> showOptions();
> 
> function showOptions() {
>     console.log(options[1]);
>     console.log(options[1].h);
>     console.log(typeof options[1]);
> }
> 
> // {id: "opt1", on: false, h: 28}
> // 28
> // object

The code I have is quite long and it gives no value for h even though 
immediately before trying to get the value console.log says it does have 
a value.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#36897

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2019-06-13 13:09 +0200
Message-ID<2720861.OsphAzSTzx@PointedEars.de>
In reply to#36892
Andrew Poulos wrote:

> On 13/06/2019 8:33 pm, Evertjan. wrote:
>> Andrew Poulos <ap_prog@hotmail.com> wrote on 13 Jun 2019 in
>> comp.lang.javascript:
>>> On 13/06/2019 5:36 pm, Thomas 'PointedEars' Lahn wrote:
>>>> Andrew Poulos wrote:
>>> var options = [
>>>                  {},
>>>                  {"id": "opt1", "on": false },
>>>                  {"id": "opt2", "on": false },
>>>                  {"id": "opt3", "on": false },
>>>                  {"id": "opt4", "on": false },
>>>                  {"id": "opt5", "on": false }
>>>                 ];
>>>
>>> for (var i = 6; i > 0; i--) {
>>>     // add stuff to options elements
>>>     options[i].h = 28;
>>> }
>>>
>>> […]
>> 
>> You did not test this, because option[6] does not exist and gives an
>> error.
> 
> Yes, you're right the 6 should've been a 5.

Then the same code will not access options[0].

> The code I have is quite long and it gives no value for h even though
> immediately before trying to get the value console.log says it does have
> a value.

If you debug it or try to reduce it to a minimal test case, you will see the 
error.  Also, ESLint might help.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#36904

FromAndrew Poulos <ap_prog@hotmail.com>
Date2019-06-13 21:54 +1000
Message-ID<pKGdnVhrbtEIpJ_AnZ2dnUU7-d3NnZ2d@westnet.com.au>
In reply to#36897
On 13/06/2019 9:09 pm, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
> 
>> On 13/06/2019 8:33 pm, Evertjan. wrote:
>>> Andrew Poulos <ap_prog@hotmail.com> wrote on 13 Jun 2019 in
>>> comp.lang.javascript:
>>>> On 13/06/2019 5:36 pm, Thomas 'PointedEars' Lahn wrote:
>>>>> Andrew Poulos wrote:
>>>> var options = [
>>>>                   {},
>>>>                   {"id": "opt1", "on": false },
>>>>                   {"id": "opt2", "on": false },
>>>>                   {"id": "opt3", "on": false },
>>>>                   {"id": "opt4", "on": false },
>>>>                   {"id": "opt5", "on": false }
>>>>                  ];
>>>>
>>>> for (var i = 6; i > 0; i--) {
>>>>      // add stuff to options elements
>>>>      options[i].h = 28;
>>>> }
>>>>
>>>> […]
>>>
>>> You did not test this, because option[6] does not exist and gives an
>>> error.
>>
>> Yes, you're right the 6 should've been a 5.
> 
> Then the same code will not access options[0].

Here 0 is meant for a different purpose.

>> The code I have is quite long and it gives no value for h even though
>> immediately before trying to get the value console.log says it does have
>> a value.
> 
> If you debug it or try to reduce it to a minimal test case, you will see the
> error.  Also, ESLint might help.

I think I understand what you're saying, and I'll run more tests, but 
what I can't understand is with this:

   console.log(options[1]);   // 1.
   console.log(options[1].h); // 2.

why 1. shows a value for h yet 2. (which immediately follows 1.) says h 
is undefined.

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#36905

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2019-06-13 17:30 +0200
Message-ID<4513428.33T0Q7i3B6@PointedEars.de>
In reply to#36904
Andrew Poulos wrote:

> […] but what I can't understand is with this:
> 
>    console.log(options[1]);   // 1.
>    console.log(options[1].h); // 2.
> 
> why 1. shows a value for h yet 2. (which immediately follows 1.) says h
> is undefined.

Very simple: You have not added the property to the object referred to by 
“options[1]”, you have added it and removed it later, or you have set it to 
“undefined”.  “JJ”’s argument is sound as well.

For example, one can create an object that cannot be augmented with 
properties:

  var obj = Object.freeze({});

  /* TypeError exception only in strict mode */
  obj.h = 42;
  
  /* undefined */
  console.log(obj.h);

This behavior actually applies to primitive strings since ECMAScript Ed. 5.
[If one wishes to call custom methods on string values, one must augment the 
String prototype, or use .call() or .apply() instead.]

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#36896

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2019-06-13 13:05 +0200
Message-ID<1580036.FruvyXNdhd@PointedEars.de>
In reply to#36882
Andrew Poulos wrote:

> var options = [
>                 {},
>                 {"id": "opt1", "on": false },
>                 {"id": "opt2", "on": false },
>                 {"id": "opt3", "on": false },
>                 {"id": "opt4", "on": false },
>                 {"id": "opt5", "on": false }
>                ];
> 
> for (var i = 6; i > 0; i--) {
>    // add stuff to options elements
>    options[i].h = 28;
> }
> 
> showOptions();
> 
> function showOptions() {
>    console.log(options[1]);        // 1.
>    console.log(options[1].h)       // 2.
>    console.log(typeof options[1]); // 3.
> 
>    // code that relies on .h values
> }
> 
> In Windows Chrome 75.0.3770.80 (64-bit):
> 
> 1.
>  > Object
> 
> when opened
> 
> v Object
>    > elem: div#opt1.opt
>      h: 28
>      id: "opt1"
>      maxw: 548
>      on: false
>    > pos: {x: 46}
>    > txt: p
>      __proto__: Object
> 
> 2. undefined
> 
> 3. object
> 
> h is in options[1] but when I try to get the value it's not.

I cannot reproduce your results.  If I run your code in

| > navigator.userAgent
| <- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) 
| Chrome/73.0.3683.75 Safari/537.36"

I get

| Uncaught TypeError: Cannot set property 'h' of undefined
|     at <anonymous>:12:17

because your code is logically in error: The referred Array instance has 6 
elements, so it does NOT have an element with index 6 (indexes start at 0)
and “options[6]” is evaluated to the *primitive* (non-object) “undefined” 
value.

Note that

  for (var i = 6; i > 0; i--)

is NOT equivalent to

  for (var i = 6; i--;)

The former checks whether the value of “i” is *greater than* zero; if so, 
the Block statement is executed.  *After* that “i” is decremented.

By contrast, the latter checks whether the value of “i” is *not equal to* 
zero, and decrements it *in any case* (to produce an *existing* index) 
*before* the Block statement is executed (only if “i” was not equal to 
zero).

The equivalent code would be (confusingly; do not do that)

  for (var i = 6; i-- > 0;)

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#36878

FromJJ <jj4public@vfemail.net>
Date2019-06-13 15:41 +0700
Message-ID<ib685gnzxgdm.1ssj38wqqhfpb$.dlg@40tude.net>
In reply to#36870
On Thu, 13 Jun 2019 16:25:58 +1000, Andrew Poulos wrote:
> I've created a property and the console it tells me that it looks like this
> 
> Object
>     elem: div#opt1.opt
>     h: 28
>     id: "opt1"
>     maxw: 548
>     on: false
>     pos: {x: 46}
>     txt: p
>     __proto__: Object
> 
> yet when, right after, I try to get the value of op.h I get undefined
> 
> console.log(op)   //--> give the object above
> console.log(op.h) //--> undefined
> 
> I don't understand how this could be happening.
> 
> Andrew Poulos

That console output looks like it came from Chrome's, but it doesn't have
the correct format. I'm guessing that the `op` variable contains a string
instead of an object.

What does the console says if you type this?

  console.log(typeof op);

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web