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


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

arr.push(obj);

Started byjonas.thornvall@gmail.com
First post2014-03-10 17:38 -0700
Last post2014-03-11 15:19 +0000
Articles 6 on this page of 46 — 7 participants

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


Contents

  arr.push(obj); jonas.thornvall@gmail.com - 2014-03-10 17:38 -0700
    Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-10 20:19 -0700
      Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-10 20:28 -0700
        Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-10 21:19 -0700
          Re: arr.push(obj); Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-03-11 06:59 -0300
            Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-11 16:02 +0100
              Re: arr.push(obj); Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-11 16:22 +0000
              Re: arr.push(obj); Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-03-11 13:32 -0300
    Re: arr.push(obj); "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-11 10:21 +0100
      Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 03:53 -0700
        Re: arr.push(obj); "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-11 14:03 +0100
          Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 07:45 -0700
            Re: arr.push(obj); "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-11 16:24 +0100
              Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 10:40 -0700
                Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 10:51 -0700
                  Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-11 19:23 +0100
                    Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 15:33 -0700
                      Re: arr.push(obj); "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-12 00:37 +0100
                        Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 16:46 -0700
                          Re: arr.push(obj); "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-12 01:05 +0100
                          Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 01:40 +0100
                      Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 00:48 +0100
                    Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 15:43 -0700
                      Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 16:18 -0700
                        Re: arr.push(obj); "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-12 00:39 +0100
                          Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 00:52 +0100
                      Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 00:49 +0100
                        Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 17:16 -0700
                          Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 02:07 +0100
                            Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 18:13 -0700
                              Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-13 02:25 +0100
                                Re: arr.push(obj); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-13 11:03 +0100
                            Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-12 07:46 -0700
                        Re: arr.push(obj); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 01:27 +0100
                          Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 01:44 +0100
            Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-11 16:26 +0100
              Re: arr.push(obj); Carlos Pedro <carlospedr@gmail.com> - 2014-03-11 09:21 -0700
              Re: arr.push(obj); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-11 18:01 +0100
                Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-11 19:15 +0100
                  Re: arr.push(obj); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-11 20:32 +0100
                    Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 01:01 +0100
                      Re: arr.push(obj); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-12 04:08 +0100
                        Re: arr.push(obj); Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-12 18:00 +0100
    Re: arr.push(obj); Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-03-11 06:24 -0300
      Re: arr.push(obj); jonas.thornvall@gmail.com - 2014-03-11 03:47 -0700
        Re: arr.push(obj); Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-11 15:19 +0000

Page 3 of 3 — ← Prev page 1 2 [3]


#23550

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-03-12 01:01 +0100
Message-ID<531fa3de$0$6656$9b4e6d93@newsspool2.arcor-online.net>
In reply to#23537
Thomas 'PointedEars' Lahn wrote:

> Christoph Michael Becker wrote:
> 
>> Thomas 'PointedEars' Lahn wrote:
>>> Christoph Michael Becker wrote:
>>>> I suggest to use ECMAScript Arrays the way they're meant to be used, and
>>>> use indexes from 0 to n, and scale them as appropriate.
>>>
>>> As long as the “length” property is not used directly, it can be useful
>>> to have an *Array* instance with user-defined properties whose names are
>>> string representations of negative numbers, positive numbers greater than
>>> 2³² − 1, and non-integers.
>>
>> While you're surely right that having "non negative" properties *can* be
>> useful sometimes, it might often be confusing (particularly for
>> beginners).
> 
> Non sequitur.  I expect people to use their brain while programming, IOW not 
> to treat those special arrays as if they were ordinary arrays.
> 
>> Not only the length property will not work as "expected",
> 
> Depends on the implementation.  You are forgetting getters.

-v, please.

>> but also several of the methods of Array.prototype, e.g. shift() and
>> unshift(). 
> 
> You could override them.
> 
>> One does not have to use these, but than one could even use
>> an object that does not "inherit" from Array at all.
> 
> But it makes sense to have an object that works like an Array in all other 
> respects.  Many Array prototype methods are intentionally generic.
> 
> <http://PointedEars.de/es-matrix?filter=Array.prototype>
> 
>> Element access via the bracket notation as well as for loops will work on 
>> such "arrays".
> 
> Depends on the implementation, see jsx.array.BigArray in 
> <http://PointedEars.de/wsvn/JSX/trunk/array.js>.
> 
> I have written this to increase the upper border for indexes (from the 
> getPrimes() discussion), and index < 0 gets you the same as in Python (with 
> the bracket property accessor: only if Proxy is supported).

Interesting. :)

> But now I am thinking of a property defining whether you can have negative 
> indexes on the object, modifying that behavior slightly.  FWIW, the “length” 
> property could then yield the difference between the maximum positive and 
> the minimum negative index or just the maximum positive index + 1 as it 
> always had;

How about having new properties or methods, say lbound and ubound.
These would be useful for iterating over the elements with for loops.


>              how did you get the idea that “for” loops would stop working for 
> those objects *per se*?

I have not got this idea.  I've written:

| Element access via the bracket notation as well as for loops will
| work on such "arrays".

-- 
Christoph M. Becker

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


#23563

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-03-12 04:08 +0100
Message-ID<1987234.4suMY2Rx6P@PointedEars.de>
In reply to#23550
Christoph Michael Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Christoph Michael Becker wrote: 
>>> Thomas 'PointedEars' Lahn wrote:
>>>> Christoph Michael Becker wrote:
>>>>> I suggest to use ECMAScript Arrays the way they're meant to be used,
>>>>> and use indexes from 0 to n, and scale them as appropriate.
>>>>
>>>> As long as the “length” property is not used directly, it can be useful
>>>> to have an *Array* instance with user-defined properties whose names
>>>> are string representations of negative numbers, positive numbers
>>>> greater than 2³² − 1, and non-integers.
>>>
>>> While you're surely right that having "non negative" properties *can* be
>>> useful sometimes, it might often be confusing (particularly for
>>> beginners).
>> 
>> Non sequitur.  I expect people to use their brain while programming, IOW
>> not to treat those special arrays as if they were ordinary arrays.
>> 
>>> Not only the length property will not work as "expected",
>> 
>> Depends on the implementation.  You are forgetting getters.
> 
> -v, please.

,----[JSX/array.js:278]
| 
|       /**
|        * @memberOf __jsx.array.BigArray
|        * @private
|        */
|       var _length = 0;
|     
|       /**
|        * Sets the real length of this array
|        *
|        * @param {int} value
|        * @throws jsx.array.RangeError
|        *   if the value is less than <code>0</code> or
|        *   greater than {@link BigArray.MAX_LENGTH}
|        */
|       this.setLength = function BigArray_setLength (value) {
|         if (value < 0 || value > _BigArray.MAX_LENGTH)
|         {
|           return _jsx.throwThis(_RangeError,
|             ["Invalid length", value,
|              "0.." + _BigArray.MAX_LENGTH
|              + " (2^" + Math.floor(Math.log(_BigArray.MAX_LENGTH)
|                           / Math.log(2)) + ")"],
|             BigArray_setLength);
|         }
| 
|         _length = Math.floor(value);
| 
|         return this;
|       };
| 
|       /**
|        * Returns the real length of this array
|        *
|        * @return {int}
|        */
|       this.getLength = function () {
|         return _length;
|       };
| 
|       /**
|        * @memberOf jsx.array.BigArray
|        * @name length
|        */
|       _defineProperty(this, "length", {
|         "get": this.getLength,
|         "set": this.setLength
|       }, "jsx.array.BigArray");
| 
`----

_defineProperty() is just an import of jsx.object.defineProperty() in 
JSX:object.js which in turn is a wrapper for Object.defineProperty() [ES5.x, 
15.2.3.6] or the proprietary ways of defining setters and getters if the 
former is unavailable.

>> But now I am thinking of a property defining whether you can have
>> negative indexes on the object, modifying that behavior slightly.
>> FWIW, the “length” property could then yield the difference between the
>> maximum positive and the minimum negative index or just the maximum
>> positive index + 1 as it always had;
> 
> How about having new properties or methods, say lbound and ubound.
> These would be useful for iterating over the elements with for loops.

Yes, much better.
 
>>              how did you get the idea that “for” loops would stop working
>>              for
>> those objects *per se*?
> 
> I have not got this idea.  I've written:
> 
> | Element access via the bracket notation as well as for loops will
> | work on such "arrays".

Sorry, I misread.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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


#23575

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-03-12 18:00 +0100
Message-ID<532092ad$0$6656$9b4e6d93@newsspool2.arcor-online.net>
In reply to#23563
Thomas 'PointedEars' Lahn wrote:

> Christoph Michael Becker wrote:
> 
>> Thomas 'PointedEars' Lahn wrote:
>>> Christoph Michael Becker wrote: 
>>>> Thomas 'PointedEars' Lahn wrote:
>>>>> Christoph Michael Becker wrote:
>>>>>> I suggest to use ECMAScript Arrays the way they're meant to be used,
>>>>>> and use indexes from 0 to n, and scale them as appropriate.
>>>>>
>>>>> As long as the “length” property is not used directly, it can be useful
>>>>> to have an *Array* instance with user-defined properties whose names
>>>>> are string representations of negative numbers, positive numbers
>>>>> greater than 2³² − 1, and non-integers.
>>>>
>>>> While you're surely right that having "non negative" properties *can* be
>>>> useful sometimes, it might often be confusing (particularly for
>>>> beginners).
>>>
>>> Non sequitur.  I expect people to use their brain while programming, IOW
>>> not to treat those special arrays as if they were ordinary arrays.
>>>
>>>> Not only the length property will not work as "expected",
>>>
>>> Depends on the implementation.  You are forgetting getters.
>>
>> -v, please.
> 
> ,----[JSX/array.js:278]
> | 
> |       /**
> |        * @memberOf __jsx.array.BigArray
> |        * @private
> |        */
> |       var _length = 0;
> |     
> |       /**
> |        * Sets the real length of this array
> |        *
> |        * @param {int} value
> |        * @throws jsx.array.RangeError
> |        *   if the value is less than <code>0</code> or
> |        *   greater than {@link BigArray.MAX_LENGTH}
> |        */
> |       this.setLength = function BigArray_setLength (value) {
> |         if (value < 0 || value > _BigArray.MAX_LENGTH)
> |         {
> |           return _jsx.throwThis(_RangeError,
> |             ["Invalid length", value,
> |              "0.." + _BigArray.MAX_LENGTH
> |              + " (2^" + Math.floor(Math.log(_BigArray.MAX_LENGTH)
> |                           / Math.log(2)) + ")"],
> |             BigArray_setLength);
> |         }
> | 
> |         _length = Math.floor(value);
> | 
> |         return this;
> |       };
> | 
> |       /**
> |        * Returns the real length of this array
> |        *
> |        * @return {int}
> |        */
> |       this.getLength = function () {
> |         return _length;
> |       };
> | 
> |       /**
> |        * @memberOf jsx.array.BigArray
> |        * @name length
> |        */
> |       _defineProperty(this, "length", {
> |         "get": this.getLength,
> |         "set": this.setLength
> |       }, "jsx.array.BigArray");
> | 
> `----
> 
> _defineProperty() is just an import of jsx.object.defineProperty() in 
> JSX:object.js which in turn is a wrapper for Object.defineProperty() [ES5.x, 
> 15.2.3.6] or the proprietary ways of defining setters and getters if the 
> former is unavailable.

Thanks.  I was not really aware of defineProperty(), let alone that
there are proprietary implementations that could be used as fallback.
Apparently something I have to catch up with.

-- 
Christoph M. Becker

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


#23515

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2014-03-11 06:24 -0300
Message-ID<lfmko8$2ll$1@speranza.aioe.org>
In reply to#23508
On 10/03/2014 21:38, jonas.thornvall@gmail.com wrote:
> 1. Can javascript have negative index of array[-500]?

The first element of an array is at index 0, and the last element is at
the index equal to the value of the array's length property minus 1.
See:
<http://www.ecma-international.org/ecma-262/5.1/#sec-15.4>
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array>

> 2. If so how can i change code below to create object range from -500
> to 500 as i understand it the code will will create an array with
> object 0-999.
>
> for (var i = -500; i <= +500; i ++ ) { var obj = { count : i, fcolor
> : 'black', rheight : 100, rwidth : 100, rcolor : 'white' }
>
> arr.push(obj); }
>
> 3. If i want to create an object on the fly as a counter
> increment/decrement in the code.
>
> Let us say obj for arr[-501] how go in todo that.
>
> JT

I didn't see an array with negative indices in the code above.

-- 
Joao Rodrigues

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


#23517

Fromjonas.thornvall@gmail.com
Date2014-03-11 03:47 -0700
Message-ID<7a7cd193-1b45-456d-ad5f-cc1780f44ce8@googlegroups.com>
In reply to#23515
Den tisdagen den 11:e mars 2014 kl. 10:24:27 UTC+1 skrev J.R.:
> On 10/03/2014 21:38, jonas.thornvall@gmail.com wrote:
> 
> > 1. Can javascript have negative index of array[-500]?
> 
> 
> 
> The first element of an array is at index 0, and the last element is at
> 
> the index equal to the value of the array's length property minus 1.
> 
> See:
> 
> <http://www.ecma-international.org/ecma-262/5.1/#sec-15.4>
> 
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array>
> 
> 
> 
> > 2. If so how can i change code below to create object range from -500
> 
> > to 500 as i understand it the code will will create an array with
> 
> > object 0-999.
> 
> >
> 
> > for (var i = -500; i <= +500; i ++ ) { var obj = { count : i, fcolor
> 
> > : 'black', rheight : 100, rwidth : 100, rcolor : 'white' }
> 
> >
> 
> > arr.push(obj); }
> 
> >
> 
> > 3. If i want to create an object on the fly as a counter
> 
> > increment/decrement in the code.
> 
> >
> 
> > Let us say obj for arr[-501] how go in todo that.
> 
> >
> 
> > JT
> 
> 
> 
> I didn't see an array with negative indices in the code above.
> 
> 
> 
> -- 
> 
> Joao Rodrigues

Thanks for your answer Joao, 

1. What i do ask is how can i store/push the objects so they are stored from indices arr[-500] and upwards -500 to 500 in the example?

2. also have need to build out the array with extra elements after it is initiated, so how can i add/push object to the first + and last- negative indice in array.

Is that even possible.

JT

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


#23523

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-03-11 15:19 +0000
Message-ID<0.efdd685900551890e56e.20140311151921GMT.87vbvkwyo6.fsf@bsb.me.uk>
In reply to#23517
jonas.thornvall@gmail.com writes:
<snip>
> 1. What i do ask is how can i store/push the objects so they are
> stored from indices arr[-500] and upwards -500 to 500 in the example?
>
> 2. also have need to build out the array with extra elements after it
> is initiated, so how can i add/push object to the first + and last-
> negative indice in array.
>
> Is that even possible.

No, not as stated, but the question is so specific that that answer is
not surprising.  You need to re-design whatever it is you are doing to
fit what the language provides.

For example, you could make an array-like object that had methods to
access and set elements offset by some defined lower-bound.

-- 
Ben.

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

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


csiph-web