2

You for sure had seen jquery. Has you know jquery allow you to make chaining of method like this:

$('.elements').css({color:'red'}).etc().etc()...

But if you notice, that function returns you a array of object(?):

$(document).ready(function(){
  var $elements = $('.c').css({color:'red'});
  console.log($elements);
});

(Example, see console to see the result)

Also the $ function does as well:

$(document).ready(function(){
  var $elements = $('.c');
  console.log($elements);
});

(Example, see console to see the result)

Both returns an array and then can keep chaining. How is that possible? Why can keep chaining when it isn't return just this(or the object itself)?

I read this post and he could succesfully add plus sign but that was to an existing string cuz he proto the string object.

Its there any way that i could archive that?

UPDATE

I know that it return a jquery object but how can i return my object that contains an array of values and keep chaining with the returned values.

UPDATE 2

What i want to archive

I have this class and i want a method to return an array of my object whenever if the chaining its over

var elements = Enumerable(anArray).Where(function(elements){ return elements != 1 })

Here the chainig has over so it must return the elements that satisfy the condition but i want to keep chaingin i just have to do this:

elements  = elements.Where(function(elements){ return elements == 1}).Take(3);

And keep chaining as looooong as I want.

Community
  • 1
  • 1
NewBiw
  • 21
  • 2
  • 2
    I'm not quite sure what your goal is or what exactly you are asking. Also, they aren't really returning an array, they are returning a jquery object which has sort of an array within it. – Smern Aug 20 '13 at 12:58
  • That what im looking, how to return an array of my object and with what ever the values are and keep chaining. I just know to to make it by return this, but not an array – NewBiw Aug 20 '13 at 13:00
  • Why? You can easily filter elements out of a jquery object or even pull a specific element... and loop through each getting values. I think you need to show your end goal for us to be able to help you further. – Smern Aug 20 '13 at 13:02
  • @smerny see my update 2 please – NewBiw Aug 20 '13 at 13:10
  • Maybe I'm still confused about what you are trying to do, but it seems like it could be done with a combination of `filter()` and `slice()`? http://jsfiddle.net/5Nv4X/2/ – Smern Aug 20 '13 at 13:17
  • You did it with Jquery, i want to do it with a my class that i create not with jquery. – NewBiw Aug 20 '13 at 13:19

2 Answers2

1

$ doesn't return an array, it returns a jQuery object, which is an array-like object. I.e. some of its properties have numeric names and it has a length property.

Example:

var obj = {
    0: element1,
    1: element2,
    find: function() {
        // some logic
        return this; // return a reference to itself for chaining
    },
    length: 2
};

Now, a jQuery method doesn't return this, it returns a new jQuery object which exposes the same interface (obviously) and hence allows chaining.

If you want to implement chaining yourself, you have to do the same: Either return this or a new instance of your object and initialize it with the data the current object has.

Example:

function Constr(data) {
    this.data = data || {};
}

Contr.prototype.chain = function() {
    // do stuff
    console.log(this.data);
    return new Constr(this.data);
    // or return this;
};

var obj = new Constr({foo: 'bar'});
obj.chain().chain().chain();
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Yes, the example I provided as example also fits for this use case. Again: Either return `this` or a new instance of your "class". – Felix Kling Aug 20 '13 at 13:39
0

a jquery object is actually a collection of html objects so when a function returns a jquery object it's actually returning more than one element.

var ChainableObject = {
    data: [],
    where: function(fn){
       //doStuff with this.data
       return this;
    },
    take: function(n){
       //doStuff with this.data
       return this;
    },
    // and so on
    //.........
}
amdorra
  • 1,536
  • 9
  • 18
  • Oh men, you saved me!!!! That whas the answer!! I'd accept you answer but i dont have reputation enougt to :( – NewBiw Aug 20 '13 at 13:35