Prototype question?

I

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;
}
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!?
 
R

RobG

Ily said:
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;
}
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){
self[j++] = self;
}
}
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...er+this+global+window&rnum=2#02130cf1fd590166

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


[...]
 
L

Lasse Reichstein Nielsen

Ily said:
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;


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 = 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
 
V

VK

Ily said:
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;
}
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)
 
R

Richard Cornford

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?

Richard.
 
V

VK

Richard said:
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>
 
R

Richard Cornford

VK said:
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.
 
T

Thomas 'PointedEars' Lahn

RobG said:
Ily said:
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;
}
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top