How to "concatenate" JavaScript functions?

2

How does lib jQuery do? Example:

$ (element) .text ('my text');

I want to know how to do a function like text() in pure JavaScript, just to learn the theory. I want to know how the html element is passed as a parameter to the text() function, which makes innerText in the same element encapsulated by the $() function.

I want to know how the text() function recognizes in which element it should add the text only being joined to $() with a (.) ??

I just use jQuery, but my will is to learn the pure JS in depth.

    
asked by anonymous 29.09.2016 / 20:46

1 answer

4

This is called fluent interface or method chainning >. Or at least it's a similar way.

The basic technique is to return the object itself being manipulated, this . The return something that can be manipulated by another function that expects an object.

You can see the source code of the example you used . And the source code of the selector (goes along with the links in> the rest of the code). In the selector you will see that it returns this which will be used as argument for the text() function.

If you want to create functions that communicate specifically with jQuery you have to follow some protocols. There are some links in Sergio's comment (in question) that show how.

English article on the subject .

    
29.09.2016 / 20:59