Chaining is generally done on the basis of the object that a method should return. If object A
be an instance of class A
and a method in it returns object A
then the returned object can again be applied to the same class and this can be arranged in a chaining fashion. Or say object of class A
returns object of class B
then, the returned object can be applied to the method of class B
.
objectOfA->methodOfA(arg)->methodOfB(args);
In this case if objectOfA->methodOfA(arg)
returns object of class B
and hence the method of class B
can be called upon it and chained as above.
In your case,
$('div.selected').html('Blah ..')
Here $('div.selected')
return a jquery element object(or array of object); upon which the method .html()
can be applied since the method is only applicable to element object of jquery. It is just chaining as other programming languages do.
In case of PHP this case looks like,
$class->method1(argument)->method(2)
In this case, if class A has two methods method1 and method2 and method1 return the instance of its own then method 2 is again applicable to it.
This can be related to a function as well. Lets us suppose I have a function as such;
def function1(name):
return name
def function2(arg):
print len(name)
Now, this two functions can be chained simply as,
function2(function1('My Name is blah ...'))
Since the function1 returns a value, the type of the value must match to the input argument of function2 and this is how object work.