473,325 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Prototype question?

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
7 1046
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
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Florian Loitsch | last post by:
hi, in section 10.1.8 (Arguments Object) it is stated that the "internal ] property of the arguments object is the orginal Object prototype object, the one that is the *initial* value of...
4
by: lkrubner | last post by:
I'm reading an essay, I think one of Crockford's, and it has this example in it: function Demo() { } Demo.prototype = new Ancestor(); Demo.prototype.foo = function () { } ; Does Ancestor now...
14
by: Mantorok Redgormor | last post by:
Would this by any chance invoke undefined behavior? extern int printf(const char *, ...); int main(void) { printf("Hello\n"); return 0; } That is, providing my own printf prototype would...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.