By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Prototype question?

P: n/a
Ily
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!?

Jan 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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....130cf1fd590166

Any attempt of mine to paraphrase Mike would only obfuscate. :-)
[...]
--
Rob
Jan 16 '06 #2

P: n/a
"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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jan 16 '06 #3

P: n/a
VK

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;
}

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!?


void myArray.splice(startIndex,-endIndex)

Jan 16 '06 #4

P: n/a
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.
Jan 16 '06 #5

P: n/a
VK

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>

Jan 17 '06 #6

P: n/a
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.
Jan 17 '06 #7

P: n/a
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
Feb 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.