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

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-14-2003, 09:52 PM   PM User | #1
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Array Functionality

An old script I recently found reasons to bring to life again...
Code:
/*\
 * Array Functionality
 * script: array.js
 * for: browsers not implementing the full array functionality of ECMA262-3
 * by: liorean <http://liorean.web-graphics.com/>
 * thanks to: beetle
\*/

if(typeof Array.prototype.copy=='undefined')
    Array.prototype.copy=function(a){
        var
            i=0,
            b=[];
        for(i;i<this.length;i++)
            b[i]=(typeof this[i].copy!='undefined')?
                this[i].copy():
                this[i];
        return b
    };

if(typeof Array.prototype.concat=='undefined')
    Array.prototype.concat=function(a){
        var
            i=0,
            b=this.copy();
        for(i;i<a.length;i++)
            b[b.length]=a[i];
        return b
    };
    
if(typeof Array.prototype.pop=='undefined')
    Array.prototype.pop=function(){
        var
            b=this[this.length-1];
        this.length--;
        return b
    };

if(typeof Array.prototype.push=='undefined')
    Array.prototype.push=function(){
        var
            i=0,
            b=this.length,
            a=arguments;
        for(i;i<a.length;i++)
            this[b+i]=a[i];
        return this.length
    };

if(typeof Array.prototype.shift=='undefined')
    Array.prototype.shift=function(){
        var
            i=0,
            b=this[0];
        for(i;i<this.length-1;i++)
            this[i]=this[i+1];
        this.length--;
        return b
    };

if(typeof Array.prototype.slice=='undefined')
    Array.prototype.slice=function(a,c){
        var
            i=0,
            b,
            d=[];
        if(!c)
            c=this.length;
        if(c<0)
            c=this.length+c;
        if(a<0)
            a=this.length-a;
        if(c<a){
            b=a;
            a=c;
            c=b
        }
        for(i;i<c-a;i++)
            d[i]=this[a+i];
        return d
    };

if(typeof Array.prototype.splice=='undefined')
    Array.prototype.splice=function(a,c){
        var
            i=0,
            e=arguments,
            d=this.copy(),
            f=a;
        if(!c)
            c=this.length-a;
        for(i;i<e.length-2;i++)
            this[a+i]=e[i+2];
        for(a;a<this.length-c;a++)
            this[a+e.length-2]=d[a-c];
        this.length-=c-e.length+2;
        return d.slice(f,f+c)
    };

if(typeof Array.prototype.unshift=='undefined')
    Array.prototype.unshift=function(a){
        var
            b;
        this.reverse();
        b=this.push(a);
        this.reverse();
        return b
    };
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 05-20-2003 at 05:16 PM..
liorean is offline   Reply With Quote
Old 05-20-2003, 05:19 AM   PM User | #2
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Nice

I'd probably want the copy() method to handle multiple dimensional arrays
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 05-20-2003, 04:07 PM   PM User | #3
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Sorry, but the only way to do that requires ECMA262-3 compliancy. This means older browasers like ie5.0 are screwed. (If I remember correctly, instanceof operator is an ECMA262-3 feature.)
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 05-20-2003, 04:28 PM   PM User | #4
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
What's wrong with doing it this way?
Code:
Array.prototype.copy = function()
{
	var
		i=0,
		b=[];
	for(i;i<this.length;i++) {
		b[i] = ( typeof this[i].copy != 'undefined' )?
			this[i].copy():
			this[i];
	}
	return b;
};
Besides, according to this page, instanceof was introduced in IE5.0 (JScript5.0) -- and this page says it was implemented in Javascript 1.4 - but don't get me lying as to which browsers support 1.4.
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”

Last edited by beetle; 05-20-2003 at 04:32 PM..
beetle is offline   Reply With Quote
Old 05-20-2003, 05:02 PM   PM User | #5
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Oh, great, more version confusion (See my discussion with Brothercake regarding the in operator in <http://codingforums.com/showthread.p...on+in+document>). In fact there's no browser supporting JavaScript 1.4, only the ONE server platform. NN4 used JavaScript 1.2 and 1.3, never 1.4. Oh, and the Sourceforge project that is based on the Netscape JavaScript engine, whatever it's name is.

Mozilla has a slightly rewritten version of the 1.4 engine that is numbered 1.5.

I'm a bit surprised that IE5 has it, but then again, Microsoft was beginning to gain on Netscape at that time.



Well, your version has better support anyway. I'll update the script.

(Lovely use of recursion, even if it isn't tail recursion, by the way.)
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 05-20-2003 at 05:16 PM..
liorean is offline   Reply With Quote
Old 05-20-2003, 05:27 PM   PM User | #6
beetle
Senior Coder

 
Join Date: Aug 2002
Posts: 3,467
Thanks: 0
Thanked 0 Times in 0 Posts
beetle has a little shameless behaviour in the past
Thanks
__________________
My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
“Minds are like parachutes. They don't work unless they are open”
“Maturity is simply knowing when to not be immature”
beetle is offline   Reply With Quote
Old 05-21-2003, 01:51 AM   PM User | #7
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
Here are two methods I had to add to Array:
Code:
 
Array.prototype.swap = function(index1,index2)
  { var temp = this[index1];
    this[index1] = this[index2];
    this[index2] = temp;
    return;
  }

Array.prototype.shuffle = function()
  { for(var i=0; i<this.length; i++)
      { ind1 = Math.floor(Math.random()*this.length);
        ind2 = Math.floor(Math.random()*this.length);
        this.swap(ind1,ind2);
      }
    return;
  }
__________________
Vladdy | KL
"Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"
Vladdy is offline   Reply With Quote
Old 06-12-2003, 04:53 PM   PM User | #8
Choopernickel
Regular Coder

 
Join Date: Apr 2003
Location: Atlanta, GA
Posts: 487
Thanks: 0
Thanked 0 Times in 0 Posts
Choopernickel is an unknown quantity at this point
My Array extensions:
Code:
Array.prototype.insert = function(newValue,position) {
    if (position && position>-1)
        this.splice(position,0,newValue);
    else
        this.push(newValue);
}

Array.prototype.insertBefore = function(newValue,beforeWhat) {
    targetIndex = this.locate(beforeWhat);
    if (targetIndex == -1)
        this.push(newValue);
    else
        this.insert(newValue,targetIndex);
}

Array.prototype.insertAfter = function(newValue,afterWhat) {
    targetIndex = this.locate(afterWhat);
    if (targetIndex == -1)
        this.push(newValue);
    else
        this.insert(newValue,targetIndex+1);
}

Array.prototype.dele = function(toDelete) {
    var tmp = this.splice(this.locate(toDelete),1);
}

Array.prototype.sum = function() {
    var i,mySum=0;
    for (i=this.length; i>0; i--)
        mySum += parseInt(this[i-1],10);
    return mySum;
}

Array.prototype.avg = function() {
    var s = this.sum();
    return s/this.length;
}

Array.prototype.max = function() {
    var m = -1;
    if (typeof this[0] == "number") m = this[0];
    for (i=this.length; i>0; i--) {
        if (typeof this[i] == "number")
            m = Math.max(m,this[i]);
    }
    return m;
}

Array.prototype.min = function() {
    var m = -1;
    if (typeof this[0] == "number") m = this[0];
    for (i=this.length; i>0; i--) {
        if (typeof this[i] == "number")
            m = Math.min(m,this[i]);
    }
    return m;
}

Array.prototype.search = function(searchFor) {
    var i=0;
    var j=0;
    var results=Array(0);
    var start = 0;
    found = -1;
    for (i=0; i<this.length; i++) {
        found = this.locate(searchFor,start);
        if (found>-1) {
            results.push(this[found]);
            start = found+1;
        }
    }
    return results;
}

Array.prototype.locate = function(what, start){
    var f = 0;
    if (start)
        startWhere = start 
    else
        startWhere = 0;
    for(f=startWhere; f<this.length; f++){
        if(this[f].toString().substr(0,what.length) == what.toString())
            return f;
    }
    return -1;
}
Choopernickel is offline   Reply With Quote
Old 06-13-2003, 07:06 AM   PM User | #9
Skyzyx
Regular Coder

 
Skyzyx's Avatar
 
Join Date: Aug 2002
Location: Silicon Valley, CA
Posts: 980
Thanks: 0
Thanked 0 Times in 0 Posts
Skyzyx is on a distinguished road
Opera 6 and 7 support JavaScript 1.4... in case anyone cares.
__________________

Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.
Skyzyx is offline   Reply With Quote
Old 06-13-2003, 09:48 AM   PM User | #10
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
opera 6 only supports a limited subset
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark
brothercake is offline   Reply With Quote
Old 11-24-2003, 05:14 PM   PM User | #11
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally posted by Skyzyx
Opera 6 and 7 support JavaScript 1.4... in case anyone cares.
Not really. They support about the same level of JavaScript as 1.4, but it's only their own approximation. I know a few details of 1.4 that no browser today supports.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 11-24-2003 at 05:17 PM..
liorean is offline   Reply With Quote
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 Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:10 AM.


Advertisement
Log in to turn off these ads.