Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #12326 > unrolled thread
| Started by | Archos <raul.san@sent.com> |
|---|---|
| First post | 2012-03-01 10:42 -0800 |
| Last post | 2012-03-01 16:24 -0800 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.javascript
Reference to slice of an array Archos <raul.san@sent.com> - 2012-03-01 10:42 -0800
Re: Reference to slice of an array "Tom de Neef" <tdeneef@qolor.nl> - 2012-03-01 20:22 +0100
Re: Reference to slice of an array Archos <raul.san@sent.com> - 2012-03-01 12:27 -0800
Re: Reference to slice of an array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-01 22:22 +0100
Re: Reference to slice of an array Scott Sauyet <scott.sauyet@gmail.com> - 2012-03-01 16:24 -0800
| From | Archos <raul.san@sent.com> |
|---|---|
| Date | 2012-03-01 10:42 -0800 |
| Subject | Reference to slice of an array |
| Message-ID | <628507ee-ddff-4106-8cdc-f2bf70d9423b@cj6g2000vbb.googlegroups.com> |
How to get a reference to a slice of an array? var A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; var s = A.slice(1, 5); // ["b", "c", "d", "e"] A[1] = 'B'; // s is always goes to have ["b", "c", "d", "e"] // but I would want could be got the value throught of a reference // so could be got ["B", "c", "d", "e"]
[toc] | [next] | [standalone]
| From | "Tom de Neef" <tdeneef@qolor.nl> |
|---|---|
| Date | 2012-03-01 20:22 +0100 |
| Message-ID | <4f4fcc8d$0$6939$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #12326 |
"Archos" <raul.san@sent.com> schreef in bericht
news:628507ee-ddff-4106-8cdc-f2bf70d9423b@cj6g2000vbb.googlegroups.com...
> How to get a reference to a slice of an array?
>
> var A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
> var s = A.slice(1, 5); // ["b", "c", "d", "e"]
>
> A[1] = 'B';
> // s is always goes to have ["b", "c", "d", "e"]
> // but I would want could be got the value throught of a reference
> // so could be got ["B", "c", "d", "e"]
Define a function ?
A.mySlice = function(){return this.slice(1,5)};
alert(A.mySlice());
Tom
[toc] | [prev] | [next] | [standalone]
| From | Archos <raul.san@sent.com> |
|---|---|
| Date | 2012-03-01 12:27 -0800 |
| Message-ID | <82deaf26-05cd-4b98-95b4-19196cabe737@hs8g2000vbb.googlegroups.com> |
| In reply to | #12327 |
On Mar 1, 7:22 pm, "Tom de Neef" <tden...@qolor.nl> wrote:
> "Archos" <raul....@sent.com> schreef in berichtnews:628507ee-ddff-4106-8cdc-f2bf70d9423b@cj6g2000vbb.googlegroups.com...
>
> > How to get a reference to a slice of an array?
>
> > var A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
> > var s = A.slice(1, 5); // ["b", "c", "d", "e"]
>
> > A[1] = 'B';
> > // s is always goes to have ["b", "c", "d", "e"]
> > // but I would want could be got the value throught of a reference
> > // so could be got ["B", "c", "d", "e"]
>
> Define a function ?
> A.mySlice = function(){return this.slice(1,5)};
> alert(A.mySlice());
>
> Tom
A.mySlice = function(l, h){return this.slice(l,h)};
var B = A.mySlice(1,5); // ["b", "c", "d", "e"]
It works, but, how to get it for that slices derived can get the
reference?
B.mySlice = function(l, h){return this.slice(l,h)};
A[3] = 33;
A.mySlice(1,5) // ["b", "c", 33, "e"]
B.mySlice(1,4) // ["c", "d", "e"]
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-03-01 22:22 +0100 |
| Message-ID | <2264524.FRqVoOGUtd@PointedEars.de> |
| In reply to | #12327 |
Tom de Neef wrote:
> "Archos" <raul.san@sent.com> schreef in bericht
> news:628507ee-ddff-4106-8cdc-f2bf70d9423b@cj6g2000vbb.googlegroups.com...
>> How to get a reference to a slice of an array?
>>
>> var A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
>> var s = A.slice(1, 5); // ["b", "c", "d", "e"]
>>
>> A[1] = 'B';
>> // s is always goes to have ["b", "c", "d", "e"]
>> // but I would want could be got the value throught of a reference
>> // so could be got ["B", "c", "d", "e"]
>
> Define a function ?
> A.mySlice = function(){return this.slice(1,5)};
> alert(A.mySlice());
You are missing the point of the question.
However, in their question the OP is missing the point as well. Unlike in
Java, the result of evaluating "b" is a primitive String value in ECMAScript
implementations like JavaScript. That value is only internally converted to
a (non-primitive) object (a String instance) when it is used with property
accessor syntax. So there can be no reference to the former value. In
addition, String values and String instances are immutable. RTFM (and
RTFFAQ, where several of them are linked) is obviously an exercise the OP
has yet to do.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-03-01 16:24 -0800 |
| Message-ID | <68d16444-9a42-4f29-89c9-07750cedaaff@d17g2000vba.googlegroups.com> |
| In reply to | #12330 |
Thomas 'PointedEars' Lahn wrote: >> "Archos" wrote: >>> How to get a reference to a slice of an array? > >>> var A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; >>> var s = A.slice(1, 5); // ["b", "c", "d", "e"] > >>> A[1] = 'B'; >>> // s is always goes to have ["b", "c", "d", "e"] >>> // but I would want could be got the value throught of a reference >>> // so could be got ["B", "c", "d", "e"] > > > However, in their question the OP is missing the point as well. Unlike in > Java, the result of evaluating "b" is a primitive String value in ECMAScript > implementations like JavaScript. That value is only internally converted to > a (non-primitive) object (a String instance) when it is used with property > accessor syntax. So there can be no reference to the former value. In > addition, String values and String instances are immutable. I'm sorry, but I'm not following your critique. As far as I can tell the OP didn't mention anything about Strings, except in the trivial sense that the elements of the array in question happened to be Strings. -- Scott
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web