Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts

    array prototype subclass

    How would I get functionality like this to work:
    it does not work that way obvious.
    Code:
    Array.prototype.2d.rotate = function()
    {
      ....
    }
    
    Array.prototype.2d.mirror= function()
    {
      ....
    }
     // etc
    it would work like... but that is not it.
    Code:
    Array.prototype.2drotate = function()
    {
      ....
    }
    Last edited by BubikolRamios; 12-01-2008 at 07:16 AM.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Property names can't begin with a number*.

    * Unless you do Array.prototype["2d"].blabla

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    ok, if I leave out number

    Code:
    Array.prototype.twod.rotate = function()
    {
      ....
    }
    it does not work either.

  • #4
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    ok, I m getting somewhere:

    call: some2dArr.twod.rotate();

    now works

    Code:
    Array.prototype.twod = function() {}
    
    Array.prototype.twod.rotate = function()
    {
       // here it says this[0] is not defined
        ..... this[0].length
    }
    any suggestion ?

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Code:
    Array.prototype.twod = function() {
        var self = this;
        return {
            rotate: function() { blablbla "self" refers to the array },
            mirror: function() { blabla }
        };
    };
    Then [bla,bla,bla].twod().rotate() would do what you want. Not quite what you had in mind though.

    Something more general might be:

    Code:
    function Matrix(arr) {
        this.array = arr;
    }
    
    Matrix.prototype.get = function(i,j) {
        return this.array[i][j];
    };
    
    Matrix.prototype.set = function(i,j, value) {
        return this.array[i][j] = value;
    };
    
    Matrix.prototype.rotate = function(angle) {
        // blablabla
    };
    
    // and so on
    Then just use your Matrix object instead.

  • Users who have thanked jkd for this post:

    BubikolRamios (12-01-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •