Prototype question?

Discussion in 'Javascript' started by Ily, Jan 16, 2006.

  1. Ily

    Ily Guest

    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!?
    Ily, Jan 16, 2006
    #1
    1. Advertising

  2. Ily

    RobG Guest

    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;
    > }
    > 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. :)


    [...]


    --
    Rob
    RobG, Jan 16, 2006
    #2
    1. Advertising

  3. "Ily" <> 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;


    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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 16, 2006
    #3
  4. Ily

    VK Guest

    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;
    > }
    > 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)
    VK, Jan 16, 2006
    #4
  5. 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.
    Richard Cornford, Jan 16, 2006
    #5
  6. Ily

    VK Guest

    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>
    VK, Jan 17, 2006
    #6
  7. 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.
    Richard Cornford, Jan 17, 2006
    #7
  8. 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;
    >> }
    >> 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
    Thomas 'PointedEars' Lahn, Feb 15, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Curley
    Replies:
    3
    Views:
    342
    David Dorward
    Oct 4, 2003
  2. June Lee
    Replies:
    2
    Views:
    786
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    172
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    257
  5. javascript fish
    Replies:
    0
    Views:
    161
    javascript fish
    Oct 11, 2008
Loading...

Share This Page