+1  A: 

I am not sure this is the best syntax to get what you want, but I suppose this is a trimmed down example ... Anyways here is a quick way to make it work

var arrayFunction = window.arrayFunction = function(array) {
 return new arrayFunction.fn.init(array);
}
arrayFunction.fn = arrayFunction.prototype = {
 init: function(array){
  var a = array;
  return  {
   someSpecialArrayFunction: function(){
    alert (a.join(' - ') ); //Super cool custom stuff here.
   }
  };
 },
}
arrayFunction( [1, 2, 3] ).someSpecialArrayFunction();
Pat
+2  A: 

Or simply:

var arrayFunction = function(array) {
    var someSpecialArrayFunction = function() {
        // do something with array
    };
    return {
        someSpecialArrayFunction: someSpecialArrayFunction
    }
};
arrayFunction( [1, 2, 3] ).someSpecialArrayFunction();

Although be careful with this, if you end up with too many methods It's probably better to use the prototype.

Luca Matteis
You should roll back your edit: it doesn't work! The array-variable in your closure will always be bound to the array passed as argument to the first call of myArrayFunction()...
Christoph
haha, you're right :)
Luca Matteis
A: 

sktrdie's answer looks nice.

An additional thing to consider would be to use the flyweight pattern instead of returning a new object instance with each call to your wrapper. The ExtJS function Ext.fly does this; it returns a globally shared instance.

Ates Goral
A: 

Try something like this:

var mathWrapper = function(array) {
    return {
        mean : function(){
         if (array.length == 0) return(0);
         var total = 0;
         for (var i = 0; i < array.length; i++) total += array[i];
         return(total / array.length);
        },
    };
}

Invoking it via something like

document.write(mathWrapper([1,2,3,4,5,6]).mean());

should return the mean of the array elements.

Dylan Beattie
This is not memory efficient - use a wrapper object and add the methods to its prototype instead...
Christoph
@Christoph - ah... suddenly a whole bunch of things that were confusing me are making sense! Thanks.
Dylan Beattie
A: 

If you don't want t change Array.prototype, the only really feasible solution is adding the methods to the specific array object:

function doSomething() {}
function doOther() {}

function wrap(array) {
    array.doSomething = doSomething;
    array.doOther = doOther;
    return array;
}

Note that I'm not defining doSomething() and doOther() within wrap() - otherwise, we would create new function objects on each call, which is highly inefficient.

A more sophisticated variant would be the following:

function doSomething() {}
function doOther() {}

function wrap(array) {
    if(this instanceof arguments.callee) {
        this.doSomething = doSomething;
        this.doOther = doOther;
    }
    else {
        arguments.callee.prototype = array;
        return new arguments.callee;
    }
}

Here, we create a new wrapper object (and don't add properties to the array itself). The wrapper object will still have access to all the array's properties. There's a slight catch, though:

var foo = wrap([1,2,3]);
document.writeln(foo.length); // writes 3
foo.push(4);
document.writeln(foo.length); // writes 4 in FF, 3 in IE
foo[4] = 5;
foo[5] = 6;
document.writeln(foo.length); // still writes 4?!

Using foo[4] = will access our wrapper object and the properties 4 and 5 will be set there and not in the array - therefore, its length property won't be updated. In IE, calling push() will already fail to correctly update the array...

If your wrapper object doesn't need to 'reroute' calls to the array object, there are other solutions using closures - sktrdie's first example is one of these. I'd argue against them because they will create new function objects with every call as well. What I would do:

function wrap(array) {
    if(this instanceof arguments.callee)
        this.array = array;
    else return new arguments.callee(array);
}

wrap.prototype.doSomething = function() {};
wrap.prototype.doOther = function() {};

Here, you can access the array in doSomething() and doOther() via this.array.

Christoph
A: 

Just extend the Array object and add your fancy methods to your object. Then you can make a bridge function that takes an array as an argument and returns your version of array. That method is by far easier and more useful.

geowa4