0

Consider an OOP model. I wanted to implement,

funct1( arg ).subfunct1( arg ); ( 

Of course like jQuery. where you have $( arg ).subfn( arg )

I have done it successfully for one subfunction. BUT,

funct1( arg ).subfunct1( arg ); // ==> works fine.
funct1( arg ).subfunct1( arg ).subfunct( arg ); // ==> doesnt work.

Source is:

var funct1=function() { 
    var dummy=new Object(); 
    dummy.subfunct1=bla bla; 
    dummy.subfunct2=bla bla; 
    return dummy; 
}

Can you say any remedies.

4 Answers 4

3

subfunct1 has to return this in order to have chainable calls.

Sign up to request clarification or add additional context in comments.

Comments

0

That only works in jQuery, because most functions are setup to return the jQuery object that they are a part of (this), after modification, so that they can be chained together.

What do your functions return?

Comments

0

There's a method to enable chainable calls on any object without return this:

function chain(obj) {
    return function() {
        var Self = arguments.callee; Self.obj = obj;
        if(arguments.length==0) return Self.obj;
        Self.obj[arguments[0]].apply(Self.obj,[].slice.call(arguments,1));
        return Self;
    }
}

Usage:

function ClassA() {
    this.prop1 = null;
    this.prop2 = null;
    this.prop3 = null;
}

ClassA.prototype = {
    method1: function(argu) {this.prop1 = argu;},
    method2: function(argu) {this.prop2 = argu;},
    method3: function(argu) {this.prop3 = argu;}
}

var a = new ClassA();
chain(a)('method1',4)('method2',5)('method3',6)();

ref: http://www.javaeye.com/topic/748348 (in Chinese)

Comments

0

Thanks for suggestions :), btw I tried a few hours as I'm not conceptually strong and got an working procedure as follows..

var funct1=function() { 
    var dummy=new Object(); 
    var clone=dummy;
    dummy.subfunct1=bla bla; //you have to return every time the clone object 
    dummy.subfunct2=bla bla; //you have to return every time the clone object 
    return dummy; 
}

thus the function (anyway) become chained to n times.
Hope this helps anyone who searches as me :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.