Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2008, 06:13 AM   PM User | #1
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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 06:16 AM..
BubikolRamios is offline   Reply With Quote
Old 12-01-2008, 06:17 AM   PM User | #2
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
Property names can't begin with a number*.

* Unless you do Array.prototype["2d"].blabla
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 12-01-2008, 06:26 AM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
ok, if I leave out number

Code:
Array.prototype.twod.rotate = function()
{
  ....
}
it does not work either.
BubikolRamios is offline   Reply With Quote
Old 12-01-2008, 06:40 AM   PM User | #4
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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 ?
BubikolRamios is offline   Reply With Quote
Old 12-01-2008, 06:56 AM   PM User | #5
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
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.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Users who have thanked jkd for this post:
BubikolRamios (12-01-2008)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:36 AM.


Advertisement
Log in to turn off these ads.