Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Prototype question?

0 views
Skip to first unread message

Ily

unread,
Jan 16, 2006, 3:50:48 AM1/16/06
to
Hi

Assume I have the following function which redimensions an array

Array.prototype.reDimension=function(startIndex, endIndex) {
var newArray1=new Array();
for (var i=0;i<this.length;i++) {
if (i<startIndex0 continue;
if (i>endIndex) break;
newArray[newArray.length]=this[i];
}
this=newArray;
}

I get an error when when I attempt the following line:
this=newArray;

the error being cannot assign to 'this'.

My question is - How can I change the above function so that its a
function of the Array class, but it changes to instance values that it
refers to?

Im sure this can be done - because the array class has push and pop
methods which internally change the number of elements the array has so
it must be possible for this to be done - the questions is how!?

RobG

unread,
Jan 16, 2006, 4:36:12 AM1/16/06
to
Ily wrote:
> Hi
>
> Assume I have the following function which redimensions an array
>
> Array.prototype.reDimension=function(startIndex, endIndex) {
> var newArray1=new Array();
> for (var i=0;i<this.length;i++) {
> if (i<startIndex0 continue;
> if (i>endIndex) break;
> newArray[newArray.length]=this[i];
> }
> this=newArray;
> }

What do you mean by "redimension" an array? If you mean reduce or increase
the length, then you can do that by changing the length property:

var anArray = ['A','B','C']; // length = 3
anArray.length = 1; // anArray is now ['A'];


Or do you mean remove elements that are undefined? e.g.

var anArray = ['A',,,'D'] // length = 4
reDimension(anArray); // anArray is now ['A','D'];


>
> I get an error when when I attempt the following line:
> this=newArray;
>
> the error being cannot assign to 'this'.

In the context above, 'this' is the global object (which is 'window' in a
browser) and no, you can't assign a new value to it.


In the following:

Array.prototype.reDimension = function(){
alert(this.length);
}

var x = new Array();


'this' refers to the array object called 'x'. If you want to remove the
undefined entries of x, then something like this will work:

<script type="text/javascript">

Array.prototype.reDimension = function(){
var self = this;
for(var i=0, j=0, k=self.length; i<k; i++){
if ('undefined' != typeof self[i]){
self[j++] = self[i];
}
}
self.length = j;
}

var x = ['A',,,'D'];
x.reDimension(); // x is now ['A','D']

</script>


But that is more like a 'compress' or 'normalise' method.

> My question is - How can I change the above function so that its a
> function of the Array class, but it changes to instance values that it
> refers to?


There is a post here from Mike Winter that gives some insight into 'this' -
it starts about halfway down, starting with:

'There are four scenarios in which the this operator value changes'

<URL:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/d77f3051f3e80a8c/02130cf1fd590166?q=michael+winter+this+global+window&rnum=2#02130cf1fd590166
>


Any attempt of mine to paraphrase Mike would only obfuscate. :-)


[...]


--
Rob

Lasse Reichstein Nielsen

unread,
Jan 16, 2006, 5:39:08 AM1/16/06
to
"Ily" <il...@igsoftwaresolutions.co.uk> writes:

> Assume I have the following function which redimensions an array
>
> Array.prototype.reDimension=function(startIndex, endIndex) {
> var newArray1=new Array();
> for (var i=0;i<this.length;i++) {
> if (i<startIndex0 continue;
> if (i>endIndex) break;
> newArray[newArray.length]=this[i];

Not too familiar with the language, I can see. What you would typically do
is:

for(var i = 0, n = endIndex - startIndex; i < n; i++) {
newArray[i] = this[startIndex + i];
}

or something similar, i.e., only loop over the elements that need to
be moved.

A much simpler approach would use the build in methods on arrays:

newArray = array.slice(startIndex, endIndex - startIndex);

> }
> this=newArray;
...


> I get an error when when I attempt the following line:
> this=newArray;
>
> the error being cannot assign to 'this'.

Which is absolutely correct. The operator "this" is not a variable.

One essential property of an object is its identity. Two objects
can have all the same property values, but if their identities
are different, they are not the same object.

What it appears you are trying to do is to make the new array assume
the identity of the original one. That's definitly not allowed.

> My question is - How can I change the above function so that its a
> function of the Array class, but it changes to instance values that it
> refers to?

It can change the properties of the array it works on, and that's
probably what it should do:

> Im sure this can be done - because the array class has push and pop
> methods which internally change the number of elements the array has so
> it must be possible for this to be done - the questions is how!?

Absolutely. There are methods for removing more elements in one operation
too.

Array.prototype.redimension = function redimension(startIndex, endIndex) {
if (this.length > endIndex) {
this.length = endIndex; // remove everything after endIndex
}
this.splice(0,startIndex); // replaces subarray 0..startIndex with nothing.
return this; // why not?
}


/L
--
Lasse Reichstein Nielsen - l...@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

VK

unread,
Jan 16, 2006, 5:45:34 AM1/16/06
to

void myArray.splice(startIndex,-endIndex)

Richard Cornford

unread,
Jan 16, 2006, 6:04:09 PM1/16/06
to
VK wrote:
<snip>
> void myArray.splice(startIndex,-endIndex)

It seems that no matter how little you post there is always something
that makes it obvious that you don't rally understand this subject at
all. Would you care to guess what it was this time?

Richard.


VK

unread,
Jan 17, 2006, 3:58:19 AM1/17/06
to

Richard Cornford wrote:
> It seems that no matter how little you post there is always something
> that makes it obvious that you don't rally understand this subject at
> all. Would you care to guess what it was this time?

My post was about to show that the functionality looked by OP is
already implemented in a core array method (splice), so there is no
need to extend prototype.

.arrayMethod(-value) supposes to work as a shortcut for
(arrayObject.length - value) but in splice it doesn't: I understand now
why. All the rest remains.

<html>
<head>
<title>Splice</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function demo() {
var arr = new Array(0,1,2,3,4,5,6,7,8,9);
var startIndex = 1;
var endIndex = 5;
arr.splice(startIndex,endIndex-startIndex);
alert(arr.splice(startIndex,endIndex-startIndex));
}

window.onload = demo;
</script>
</head>

<body>

</body>
</html>

Richard Cornford

unread,
Jan 17, 2006, 8:51:28 AM1/17/06
to
VK wrote:
> Richard Cornford wrote:
>> VK wrote:
>>> void myArray.splice(startIndex,-endIndex)

>>
>> It seems that no matter how little you post there is always
>> something that makes it obvious that you don't rally understand
>> this subject at all. Would you care to guess what it was this
>> time?
>
> My post was about to show that the functionality looked by
> OP is already implemented ...

Your reasons from posting your nonsense are not relavant.

> .arrayMethod(-value) supposes to work as a shortcut for

> (arrayObject.length - value) but in splice it doesn't: ...
<snip>

No, that wasn't it. You are looking from something that makes it
_obvious_ that you don't know what you are talking about. Have another
go, that expression statement is not so long that it should take you
more than a couple of guesses.

Richard.


Thomas 'PointedEars' Lahn

unread,
Feb 15, 2006, 10:09:05 AM2/15/06
to
RobG wrote:

> Ily wrote:
>> Assume I have the following function which redimensions an array
>>
>> Array.prototype.reDimension=function(startIndex, endIndex) {
>> var newArray1=new Array();
>> for (var i=0;i<this.length;i++) {
>> if (i<startIndex0 continue;
>> if (i>endIndex) break;
>> newArray[newArray.length]=this[i];
>> }
>> this=newArray;
>> }
>

> What do you mean by "redimension" an array? [...]

I think that this term is from Visual Basic (.NET) where the ReDim statement
is "Used at procedure level to reallocate storage space for an array
variable."

<URL:http://msdn.microsoft.com/library/en-us/vblr7/html/vastmReDim.asp>


PointedEars

0 new messages